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}});