查询
db.testCo2.find();
db.testCo2.findOne();
db.testCo2.find().limit(1);
带条件
db.testCo2.find({"name":"cgh21"});
db.testCo2.find({name:/cgh/}); //like ‘%123%’
范围查询
db.testCo2.find({"$and":[{age:{"$lt":30}},{age:{"$gt":25}}]});
分页 从skip 条记录开始 (index 同数组)
db.testCo2.find().skip(1).limit(2);
db.testCo2.find().sort({"age":1}).skip(50).limit(5);
count
db.testCo2.find().count();
db.testCo2.find({"age":{"$gt":33}}).count();
db.testCo2.count({"age":{"$lt":80}});
排序
db.testCo2.find().sort({"age":1}); 1升序-1降序(后面-前面)
多字段
db.testCo2.find({"$and":[{age:22},{name:"cgh12"}]});或$or
insert
db.testCo2.insert({"name":"cgh22","age":22});
remove
db.testCo2.remove({"name":"cgh22"});
update
db.testCo2.update({"name":"cgh22"},{"$set":{"age":22}},false,true);
db.testCo2.update({"name":"cgh22"},{"$push":{"add_cloumn":"rand_str"}}); //新增字段add_cloumn
查询修改
db.testCo2.findAndModify({
query:{age:{$gte:88}},
update:{$set:{name:"cgh"},$inc:{age:2}}
});
db.runCommand({findandmodify:”testCo2”,
query:{age:{$gte:88}},
update:{$set:{name:"cgh"},$inc:{age:2}}
});