网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

【MongoDB学习笔记15】MongoDB的查询:find查询条件

find除了精确查询外,可以匹配更多的条件;

创新互联建站坚持“要么做到,要么别承诺”的工作理念,服务领域包括:做网站、网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的站前网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

一、比较操作符

$lt代表<;

$lte代表<=;

$gt代表>;

$gte代表>=;

> db.post.find()   
{ "_id" : ObjectId("54a530c3ff0df3732bac1681"), "id" : 2, "name" : "joe", "age" : 30, "sex" : 1, "school" : "marry" }    
{ "_id" : ObjectId("54a530c3ff0df3732bac1680"), "id" : 1, "name" : "joe", "age" : 30, "comments" : [ "test2", "test9", "test5" ], "sex" : 1, "school" : "marry" }    
{ "_id" : ObjectId("54a9700e1b5afd45354fd086"), "id" : 3, "test3" : 3 }    
{ "_id" : ObjectId("54a9701c1b5afd45354fd087"), "id" : 4, "test4" : 4 }    
{ "_id" : ObjectId("54a970281b5afd45354fd088"), "id" : 5, "test5" : 5 }    
{ "_id" : ObjectId("54a970351b5afd45354fd089"), "id" : 6, "test6" : 6 }    
{ "_id" : ObjectId("54a970781b5afd45354fd08a"), "id" : 7, "test7" : 7 }    
{ "_id" : ObjectId("54a970831b5afd45354fd08b"), "id" : 8, "test8" : 8 }    
{ "_id" : ObjectId("54a970901b5afd45354fd08c"), "id" : 9, "test9" : 9 }    
{ "_id" : ObjectId("54a9709c1b5afd45354fd08d"), "id" : 10, "test10" : 10 }    
> db.post.find({"id":{"$gte":5,"$lte":7}})    
{ "_id" : ObjectId("54a970281b5afd45354fd088"), "id" : 5, "test5" : 5 }    
{ "_id" : ObjectId("54a970351b5afd45354fd089"), "id" : 6, "test6" : 6 }    
{ "_id" : ObjectId("54a970781b5afd45354fd08a"), "id" : 7, "test7" : 7 }    
>

$ne代表不等于:

> db.post.find({"id":{"$ne":8}})   
{ "_id" : ObjectId("54a530c3ff0df3732bac1681"), "id" : 2, "name" : "joe", "age" : 30, "sex" : 1, "school" : "marry" }    
{ "_id" : ObjectId("54a530c3ff0df3732bac1680"), "id" : 1, "name" : "joe", "age" : 30, "comments" : [ "test2", "test9", "test5" ], "sex" : 1, "school" : "marry" }    
{ "_id" : ObjectId("54a9700e1b5afd45354fd086"), "id" : 3, "test3" : 3 }    
{ "_id" : ObjectId("54a9701c1b5afd45354fd087"), "id" : 4, "test4" : 4 }    
{ "_id" : ObjectId("54a970281b5afd45354fd088"), "id" : 5, "test5" : 5 }    
{ "_id" : ObjectId("54a970351b5afd45354fd089"), "id" : 6, "test6" : 6 }    
{ "_id" : ObjectId("54a970781b5afd45354fd08a"), "id" : 7, "test7" : 7 }    
{ "_id" : ObjectId("54a970901b5afd45354fd08c"), "id" : 9, "test9" : 9 }    
{ "_id" : ObjectId("54a9709c1b5afd45354fd08d"), "id" : 10, "test10" : 10 }    
>

$in可以查询多个键值:

> db.post.find({"id":{"$in":[4,2,8]}})   
{ "_id" : ObjectId("54a530c3ff0df3732bac1681"), "id" : 2, "name" : "joe", "age" : 30, "sex" : 1, "school" : "marry" }    
{ "_id" : ObjectId("54a9701c1b5afd45354fd087"), "id" : 4, "test4" : 4 }    
{ "_id" : ObjectId("54a970831b5afd45354fd08b"), "id" : 8, "test8" : 8 }    
>

 

$nin用法:

> db.post.find({"id":{"$nin":[4,2,8]}})   
{ "_id" : ObjectId("54a530c3ff0df3732bac1680"), "id" : 1, "name" : "joe", "age" : 30, "comments" : [ "test2", "test9", "test5" ], "sex" : 1, "school" : "marry" }    
{ "_id" : ObjectId("54a9700e1b5afd45354fd086"), "id" : 3, "test3" : 3 }    
{ "_id" : ObjectId("54a970281b5afd45354fd088"), "id" : 5, "test5" : 5 }    
{ "_id" : ObjectId("54a970351b5afd45354fd089"), "id" : 6, "test6" : 6 }    
{ "_id" : ObjectId("54a970781b5afd45354fd08a"), "id" : 7, "test7" : 7 }    
{ "_id" : ObjectId("54a970901b5afd45354fd08c"), "id" : 9, "test9" : 9 }    
{ "_id" : ObjectId("54a9709c1b5afd45354fd08d"), "id" : 10, "test10" : 10 }    
>

$or的用法:

> db.post.find({"$or":[{"sex":1},{"id":5}]})   
{ "_id" : ObjectId("54a530c3ff0df3732bac1681"), "id" : 2, "name" : "joe", "age" : 30, "sex" : 1, "school" : "marry" }    
{ "_id" : ObjectId("54a530c3ff0df3732bac1680"), "id" : 1, "name" : "joe", "age" : 30, "comments" : [ "test2", "test9", "test5" ], "sex" : 1, "school" : "marry" }    
{ "_id" : ObjectId("54a970281b5afd45354fd088"), "id" : 5, "test5" : 5 }    
>

   
$mod会将查询的值除以第一个给定的值,若余数匹配第二个值,则匹配成功;

> db.post.find()   
{ "_id" : ObjectId("54a530c3ff0df3732bac1681"), "id" : 2, "name" : "joe", "age" : 30, "sex" : 1, "school" : "marry" }    
{ "_id" : ObjectId("54a530c3ff0df3732bac1680"), "id" : 1, "name" : "joe", "age" : 30, "comments" : [ "test2", "test9", "test5" ], "sex" : 1, "school" : "marry" }    
{ "_id" : ObjectId("54a9700e1b5afd45354fd086"), "id" : 3, "test3" : 3 }    
{ "_id" : ObjectId("54a9701c1b5afd45354fd087"), "id" : 4, "test4" : 4 }    
{ "_id" : ObjectId("54a970281b5afd45354fd088"), "id" : 5, "test5" : 5 }    
{ "_id" : ObjectId("54a970351b5afd45354fd089"), "id" : 6, "test6" : 6 }    
{ "_id" : ObjectId("54a970781b5afd45354fd08a"), "id" : 7, "test7" : 7 }    
{ "_id" : ObjectId("54a970831b5afd45354fd08b"), "id" : 8, "test8" : 8 }    
{ "_id" : ObjectId("54a970901b5afd45354fd08c"), "id" : 9, "test9" : 9 }    
{ "_id" : ObjectId("54a9709c1b5afd45354fd08d"), "id" : 10, "test10" : 10 }    
{ "_id" : ObjectId("54aa8a90652d8bdfa0566d34"), "id" : 11, "test10" : 11 }    
> db.post.find({"id":{$mod:[5,1]}})    
{ "_id" : ObjectId("54a530c3ff0df3732bac1680"), "id" : 1, "name" : "joe", "age" : 30, "comments" : [ "test2", "test9", "test5" ], "sex" : 1, "school" : "marry" }    
{ "_id" : ObjectId("54a970351b5afd45354fd089"), "id" : 6, "test6" : 6 }    
{ "_id" : ObjectId("54aa8a90652d8bdfa0566d34"), "id" : 11, "test10" : 11 }    
>


名称栏目:【MongoDB学习笔记15】MongoDB的查询:find查询条件
文章分享:http://bjjierui.cn/article/jpodsh.html

其他资讯