1,分组合并:http://blog.csdn.net/mcpang/article/details/7752736
$group : { _id : "$author", books: { $push: "$title" }
2,合并数组分成行:
{ $unwind : "$title" }
3,match后分组累计:
{$match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] }},
{ $group: { _id: null, count: { $sum: 1 } } }
{ $sort : { age : -1, posts: 1 } }
4,{"a.b": {$exists: true}} 字段存在
5,db.xxx.find({E: {$gte: ISODate("2014-05-29")}}).count()累计
6,db.test.update({'_id': 'cswuyg'}, {$set: {'super_admin': true}})
7,删除增加key
xx.update({'_id': 'c'}, {$unset: {'b': {$exists: true}}})
{ "_id" : "c", "a" : { "b" : 12 }, "b" : 7 } ==> { "_id" : "c", "a" : { "b" : 12 } }
xx.update({'_id': 'z'}, {'$set': {'hello': 'z'}})
{ "_id" : "z", "b" : 1 }==>{ "_id" : "z", "b" : 1, "hello" : "z" }
8,增加数字,采用$inc
update({"a.b": {$exists: true}}, {$inc: {'a.b': 2}})
9,不存在是否插入true,是否批量true
db.xxx.update({"_id": {$exists: 1}}, {$set: {"_" : ISODate("2014-03-21T00: 00:00Z")}}, true, true)
update({"_id": "wyg"}, {"$set": {"a": "c"}, "$addToSet": {"add": {"$each" :["a", "c"]}}}, true)
10.$addToSet不重复,$each不把list作为一个元素插入,变成了2维数组$pushAll相当于$push+$each
.update({"a": 1}, {$push: {"name": {$each:["a", "c"]}}})
.update({"a": 1}, {$addToSet: {"name": {$each: ["a", "d"]}}})
.update({"a":"b"}, {$pushAll:{"c": ["z", "d"]}})
11
更新或插入
update({'_id': 'id'}, {'$setOnInsert': {'a': 'a'}, '$set': {'b': 'b'}}, true)
.update({"_id": "abc"}, {$push: {"name": "c"}, $setOnInsert: {"cc":"xx"}}, true)
posted on
浙公网安备 33010602011771号