3 对数据进行的一些写入和更新的操作
跟表添加或更新某个字段
res = await SS_ClassRoom.update_one( { "$and": [ {"Class_Room_Uid": self.Class_Room_Uid}, {"Rucode": self.Rucode} ] }, { "$set": { "LoginStatus": ConstStatusLogout # 0 未登录 1 登录 } } )
在表中插入文档
newRecord = { "StudentMid": p["StudentMid"], "StudentGroupMid": p["StudentGroupMid"], "AttendenceType": p["AttendenceType"], #1.正常 2.迟到 3.请假 4.旷课 "Lessons": p["Lessons"], "Subject": p["Subject"], "PublicClass": p["PublicClass"], "PunchDateTime": datetime.strptime(p["PunchDateTime"], "%Y-%m-%d %H:%M:%S"), "CreateDateTime": datetime.utcnow() } # 插入处理 try: oldRecord = await SS_AttendenceLog.find_one(filter=dupRecodFilter, sort=[('Created_Time', -1)]) delRes = await SS_AttendenceLog.delete_many(filter=dupRecodFilter) newRes = await SS_AttendenceLog.insert_one(newRecord) except Exception as ex: self.resData["failed"].append(p) continue
MongoDB的upsert状态判断和pymongo使用方法
在mongo中,有一个命令非常的方便,就是upsert,顾名思义就是update+insert的作用
根据条件判断有无记录,有的话就更新记录,没有的话就插入一条记录
upsert的使用方法:
db.user.insert({"name":"user1", "age":12, "sex":"male"}) db.user.insert({"name":"user2", "age":13, "sex":"male"}) db.user.insert({"name":"user3", "age":14, "sex":"male"})
查询
db.user.find()
{ "_id" : ObjectId("54c75876662ee1617463f0a3"), "name" : "user1", "age" : 12, "sex" : "male" } { "_id" : ObjectId("54c75880662ee1617463f0a4"), "name" : "user2", "age" : 13, "sex" : "male" } { "_id" : ObjectId("54c7588d662ee1617463f0a5"), "name" : "user3", "age" : 14, "sex" : "male" }
更新
db.user.update({"name":"user1"}, {"$set":{"age":21}}, {"upsert":"true"}) db.user.find()
查需结果
{ "_id" : ObjectId("54c75876662ee1617463f0a3"), "name" : "user1", "age" : 21, "sex" : "male" } { "_id" : ObjectId("54c75880662ee1617463f0a4"), "name" : "user2", "age" : 13, "sex" : "male" } { "_id" : ObjectId("54c7588d662ee1617463f0a5"), "name" : "user3", "age" : 14, "sex" : "male" }
# 判断如果找到,就更新,没找到就执行插入的操作(这里因为找到了user1,所以只是更新,不会执行$setOneInsert这条语句)
db.user.update({"name":"user1"}, {"$set":{"age":21}, "$setOnInsert":{"sex":"female"}}, {"upsert":"true"})
db.user.find()
{ "_id" : ObjectId("54c75876662ee1617463f0a3"), "name" : "user1", "age" : 21, "sex" : "male" } { "_id" : ObjectId("54c75880662ee1617463f0a4"), "name" : "user2", "age" : 13, "sex" : "male" } { "_id" : ObjectId("54c7588d662ee1617463f0a5"), "name" : "user3", "age" : 14, "sex" : "male" }
#这里执行的的是插入的操作,因为没有找到user4
db.user.update({"name":"user4"}, {"$set":{"age":21}, "$setOnInsert":{"sex":"female"}}, {"upsert":"true"})\
db.user.find()
查询结果
{ "_id" : ObjectId("54c75876662ee1617463f0a3"), "name" : "user1", "age" : 21, "sex" : "male" } { "_id" : ObjectId("54c75880662ee1617463f0a4"), "name" : "user2", "age" : 13, "sex" : "male" } { "_id" : ObjectId("54c7588d662ee1617463f0a5"), "name" : "user3", "age" : 14, "sex" : "male" } { "_id" : ObjectId("54c75a07478a805237d087c3"), "name" : "user4", "age" : 21, "sex" : "female" }
在一个数据库中全额插入一个字段,默认的类型为int
db.SS_School.update({},{$set: {"AttendenceType":NumberInt(1)}},{multi: true})
在mongod中使用正则进行检索
db.getCollection('SS_School').find({$or:[{"SchoolName":{$regex:"网络"},"SchoolId":{$regex:"340"}}]})
if SearchFiled: _fiter = {"$or": [{"SchoolName": {'$regex': SearchFiled}}, {"SchoolId": {'$regex': SearchFiled}},], 'AttendenceType': 1} count = await School.count(filter=_fiter) SchoolRes = await School.find(filter=_fiter,per_page=per_page, page=page) else: _fiter = {"AttendenceType": 1} SchoolRes = await School.find(filter=_fiter, per_page=per_page, page=page) count = await School.count(filter=_fiter)
在同一个主机下,把Mongo中的一个数据库里的表复制到另一个数据库中
db.<collection_name>.find().forEach(function(d){ db.getSiblingDB('<new_database>')['<collection_name>'].insert(d); });
修改mongo中的字段名
db.getCollection('character').update({},{$rename:{"desc":"DESC"}},false,true)
删除表中的一个字段
db.User.update({},{$unset:{'address':''}},false, true)