using mongodb advanced command 2
2013-06-03 00:00
344 查看
文章来自我的博客: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 }
#分页,只显示指定的条数
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 }
相关文章推荐
- using mongodb advanced command 3
- using mongodb advanced command 1
- using mongodb command
- Using where conditions in mysqldump command
- Using the Code Coverage Tools from the Command Line
- How to Take Ownership of a File/Folder Using Command-Line in Windows 7 or newer version System
- Start OpenOffice.org using the command line
- Find Any AT Command For iPhone Baseband Using My List
- mysql5.0 Using a password on the command line interface can be insecure.最简单的解决办法
- vs2010 command prompt 运行时停在Setting environment for using Microsoft Visual Studio 2010 x86 tools.
- Compiling JSPs Using the Command-Line Compiler---官方
- MongoDB的golang驱动安装 + 首次调用Mongo Command。
- Firebug Tutorial (Section 1)– Using Commandline API in Firebug (Part III)
- can I read/write the routing table in C without using system() command?
- Using Android Phone to recover SD card formatted with DD command under linux
- How to generate self-signed certificate using keytool command of JDK?
- Could not execute auto check for display colors using command /usr/bin/xdpyinfo
- First Impression on BBED: explore block structure using map command
- Social Data and Log Analysis Using MongoDB
- Linux - fsck命令修复文件系统 | Using command fsck to repair file system