pymongo 操作mongo数据库插入:
insert_one:插入一条
$set:值替换;$inc :自增/自减
new_data = await db.hikrayin_resKeyTask_Collect.insert_one(collectData.dict())
MongoDB 聚合操作
$unwind:拆分数组为一条条数据

db.hikrayin_resKeyTask_SubTask.aggregate([
{
'$match': {
'_id': Data.subTaskId
}
}, {
'$unwind': {
'path': '$feedBacks',
'includeArrayIndex': 'feedBackIndex',
'preserveNullAndEmptyArrays': True
}
}, {
'$match': {
'feedBacks.isShow': True
}
}, {
'$sort': {
'feedBackIndex': 1
}
}, {
'$group': {
'_id': None,
'manPower': {
'$sum': '$feedBacks.manPowerinput'
},
'NewestFeedBack': {
'$last': '$feedBacks'
}
}
}
])
查询数组是否包含:
{
id: 1,
arr: [
"123",
"456",
"789"
]
}
db.test.find({arr:{$elemMatch:{$eq:"456"}}})
查找是否存在某字段:
dbfindDocumentContainsSpecificFieldDemofind({"ProductPricesProduct2":{$exists:true}});
本文介绍了如何使用Python的pymongo库向MongoDB数据库插入数据,包括insert_one方法和文档更新操作如$set和$inc。此外,文章还展示了MongoDB的聚合操作,通过$unwind拆分数组,$match筛选,$sort排序,$group求和以及$last获取最新数据。同时,提到了查询数组中是否存在特定元素和检查字段是否存在的方法。
1173

被折叠的 条评论
为什么被折叠?



