循环删除
db.my_collection_name.find({"msgId":"myMsgId"}).forEach(function(doc){
db.getCollection('my_collection_name').remove({_id: doc._id})});
循环插入
db['my_collection_name'].find({dbCreateTime:{$lt:ISODate("2022-07-01 00:00:00")}})
.forEach(function(x){db['my_collection_name_202203'].insert(x);})
创建索引
db.getCollection("my_collection_name").createIndex({ "templateCode": 1 }, {
"name": "idx_templateCode",
"background": true
})
更新数据
db.getSiblingDB("myDb").getCollection("myTable").updateOne({"_id": ObjectId("myId")},{$set:{"myColumn":"myName"}})
exists
db.myTable.find({
templateCode: {
$exists: true
}
})
指定返回字段
db.myTable.find({
tags: {
$exists: true
}
}, {
msgId: 1,
tags: 1,
dbCreateTime: 1
}).limit(2000)
group by + count
db.message.aggregate([
{ $group: { _id: "$templateCode", total: { $sum: 1 } } },
{ $sort: { total: -1 } }
])
in
db.message.find({"templateCode":{$in:['bb','aa']}}).limit(20)
not in
db.message.find({"templateCode":{$nin:['bb','aa']}}).limit(20)