文章来自我的博客:https://www.lwxshow.com/db/mongo/using-mongodb-advanced-command-2.html
#分页,只显示指定的条数
db.cuser.find({age:{$ne:5}}).limit(5);
#跳过3个 然后显示5条数据
db.cuser.find({age:{$ne:5}}).skip(3).limit(5);
#显示集合中一共有多少条数据
> db.cuser.count();
8
> db.cuser.find().count();
8
#mongodb排序 1升序 2降序
db.cuser.find().sort({age:-1});#按年龄降序
db.cuser.find().sort({age:1});#按年龄升序
#条件为age不等于5,从第四个开始,显示5条数据 升序 可用于分页
db.cuser.find({age:{$ne:5}}).skip(3).limit(5).sort({age:1});;
#统计个数
db.cuser.find({age:{$ne:5}}).skip(3).limit(5).sort({age:1}).count(0);
上面等于
db.cuser.find({age:{$ne:5}}).skip(3).limit(5).sort({age:1}).count();
db.cuser.find().count();#所有的数据
#按条件筛选后还有多少条
db.cuser.find({age:{$ne:5}}).skip(3).limit(5).sort({age:1}).count(1);;
skip(),sort(), limit();使用不分先后
#包含 $all
db.cuser.find({age:{$all:[3]}});
{ "_id" : ObjectId("51a23348ea214a8ee3204de3"), "name" : "userid_3", "pwd" : "pwd_3", "age" : 3 }
#先更新数据 添加一个用户提交帖子内容
db.cuser.update({name:"userid_1"},{$set:{post:[1,2,3,4,5]}});
#查找帖子包含1,2,3的数据
> db.cuser.find({post:{$all:[1,2,3]}});
{ "_id" : ObjectId("51a23348ea214a8ee3204de1"), "age" : 1, "name" : "userid_1", "post" : [ 1, 2, 3, 4, 5 ], "pwd" : "pwd_1" }
#取模的 $mod 年龄
db.cuser.find({age:{$mod:[2,1]}});#取2 余1的
#取2 余1的输出结果
db.cuser.find({age:{$mod:[2,1]}});
{ "_id" : ObjectId("51a23348ea214a8ee3204de3"), "name" : "userid_3", "pwd" : "pwd_3", "age" : 3 }
{ "_id" : ObjectId("51a23348ea214a8ee3204de5"), "name" : "userid_5", "pwd" : "pwd_5", "age" : 5 }
{ "_id" : ObjectId("51a23348ea214a8ee3204de7"), "name" : "userid_7", "pwd" : "pwd_7", "age" : 7 }
{ "_id" : ObjectId("51a23348ea214a8ee3204de1"), "age" : 1, "name" : "userid_1", "post" : [ 1, 2, 3, 4, 5 ], "pwd" : "pwd_1" }
#取2 余0的输出结果
> db.cuser.find({age:{$mod:[2,0]}});
{ "_id" : ObjectId("51a23348ea214a8ee3204de2"), "name" : "userid_2", "pwd" : "pwd_2", "age" : 2 }
{ "_id" : ObjectId("51a23348ea214a8ee3204de4"), "name" : "userid_4", "pwd" : "pwd_4", "age" : 4 }
{ "_id" : ObjectId("51a23348ea214a8ee3204de6"), "name" : "userid_6", "pwd" : "pwd_6", "age" : 6 }
{ "_id" : ObjectId("51a23348ea214a8ee3204de8"), "name" : "userid_8", "pwd" : "pwd_8", "age" : 8 }