Mongodb使用命令总结

使用时的Mongodb版本为:3.2

1、导出:

mongoexport --host mongodb1.example.net --port 37017 --username user --password pass --collection contacts --db marketing --out mdb1-examplenet.json

2、导入:

mongoimport --host mongodb1.example.net --port 37017 --username user --password pass --collection contacts --db marketing --file /opt/backups/mdb1-examplenet.json

3、多条件查找:

db.collection.find({lang:"zh",createtime:{$gte:new Date("3/31/2015 6:52:46")}});
{"liju":{$ne:null},"liju.mediaid":{$ne:null}};

4、查询并排序:

db.collection.find().sort( { age: -1 } )-1为降序,1为正序
db.collection.find( { $query: {}, $orderby: { age : -1 } } )

5、批量更新:

var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { status: "D" } ).update( { $set: { status: "I", points: "0" } } );
bulk.find( { item: null } ).update( { $set: { item: "TBD" } } );
bulk.execute();
posted @ 2017-02-03 17:29  BrisWhite  阅读(165)  评论(0编辑  收藏  举报