mongodb查询的语法
本文参考自官方的手册:
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-ConditionalOperators%3A%3C%2C%3C%3D%2C%3E%2C%3E%3D
1 ) . 大于,小于,大于或等于,小于或等于
$gt:大于
$lt:小于
$gte:大于或等于
$lte:小于或等于
例子:
db.collection.find({ "field"
: { $gt: value } } );
db.collection.find({ "field"
: { $lt: value } } );
db.collection.find({ "field"
: { $gte: value } } );
db.collection.find({ "field"
: { $lte: value } } );
如查询j大于3,小于4:
db.things.find({j : {$lt: 3}});
db.things.find({j : {$gte: 4}});
也可以合并在一条语句内:
db.collection.find({ "field"
: { $gt: value1, $lt: value2 } } );
2) 不等于 $ne
例子:
db.things.find( { x : { $ne : 3 } } );
3) in 和 not in ($in $nin)
语法:
db.collection.find( { "field"
: { $in : array } } );
例子:
db.things.find({j:{$in: [2,4,6]}});
db.things.find({j:{$nin: [2,4,6]}});
4) 取模运算$mod
如下面的运算:
db.things.find( "this
.a % 10 == 1"
)
可用$mod代替:
db.things.find( { a : { $mod : [ 10 , 1 ] } } )
5) $all
$all和$in类似,但是他需要匹配条件内所有的值:
如有一个对象:
下面这个条件是可以匹配的:
db.things.find( { a: { $all: [ 2, 3 ] } } );
但是下面这个条件就不行了:
db.things.find( { a: { $all: [ 2, 3, 4 ] } } );
6) $size
$size是匹配数组内的元素数量的,如有一个对象:{a:["foo"]
},他只有一个元素:
下面的语句就可以匹配:
db.things.find( { a : { $size: 1 } } );
官网上说不能用来匹配一个范围内的元素,如果想找$size<5之类的,他们建议创建一个字段来保存元素的数量。
You cannot use $size
to find a range of sizes (for example: arrays with more than 1 element). If you need to query for a range, create an extra size
field that you increment when you add elements.
7)$exists
$exists用来判断一个元素是否存在:
如:
db.things.find( { a : { $exists : true
} } );
db.things.find( { a : { $exists : false
} } );
8) $type
$type
基于 bson
type来匹配一个元素的类型,像是按照类型ID来匹配,不过我没找到bson类型和id对照表。
db.things.find( { a : { $type : 2 } } );
db.things.find( { a : { $type : 16 } } );
9)正则表达式
mongo支持正则表达式,如:
db.customers.find( { name : /acme.*corp/i } ); // 后面的i的意思是区分大小写
10) 查询数据内的值
下面的查询是查询colors内red的记录,如果colors元素是一个数据,数据库将遍历这个数组的元素来查询。
db.things.find( { colors : "red"
} );
11) $elemMatch
如果对象有一个元素是数组,那么$elemMatch可以匹配内数组内的元素:
> t.find( { x : { $elemMatch : { a : 1, b : { $gt : 1 } } } } )
{ "_id"
: ObjectId("4b5783300334000000000aa9"
),
"x"
: [ { "a"
: 1, "b"
: 3 }, 7, { "b"
: 99 }, { "a"
: 11 } ]
}
$elemMatch : { a : 1, b : { $gt : 1 } } 所有的条件都要匹配上才行。
注意,上面的语句和下面是不一样的。
> t.find( { "x.a"
: 1, "x.b"
: { $gt : 1 } } )
$elemMatch是匹配{ "a"
: 1, "b"
: 3 },而后面一句是匹配{ "b"
: 99 }, { "a"
: 11 }
12) 查询嵌入对象的值
db.postings.find( { "author.name"
: "joe"
} );
注意用法是author.name
,用一个点就行了。更详细的可以看这个链接: dot notation
举个例子:
> db.blog.save({ title : "My First Post"
, author: {name : "Jane"
, id : 1}})
如果我们要查询 authors name 是Jane的, 我们可以这样:
> db.blog.findOne({"author.name"
: "Jane"
})
如果不用点,那就需要用下面这句才能匹配:
db.blog.findOne({"author"
: {"name"
: "Jane"
, "id"
: 1}})
下面这句:
db.blog.findOne({"author"
: {"name"
: "Jane"
}})
是不能匹配的,因为mongodb对于子对象,他是精确匹配。
13) 元操作符 $not 取反
如:
db.customers.find( { name : { $not : /acme.*corp/i } } );
db.things.find( { a : { $not : { $mod : [ 10 , 1 ] } } } );
mongodb还有很多函数可以用,如排序,统计等,请参考原文。
mongodb目前没有或(or)操作符,只能用变通的办法代替,可以参考下面的链接:
http://www.mongodb.org/display/DOCS/OR+operations+in+query+expressions
以上出自:http://hi.baidu.com/farmerluo/blog/item/2d15e95027aa86501138c27a.html
-
-
BasicDBObject or1 = new
BasicDBObject();
-
or1.put("x"
,
new
BasicDBObject(
"$gt"
,
1002
));
-
-
BasicDBObject or2 = new
BasicDBObject();
-
or2.put("x"
,
new
BasicDBObject(
"$lt"
,
204
));
-
-
ArrayList<DBObject> list = new
ArrayList<DBObject>();
-
list.add(or1);
-
list.add(or2);
-
query.put("$or"
,list);
-
-
-
query.put("y"
,
new
BasicDBObject(
"$gt"
,
63
));
以上的mongoDB语法对应到sql就是
-
where
(x > 1002
or
x < 204)
and
y > 63
以上出自:http://www.iteye.com/topic/1008790
分享到:
相关推荐
NoSQL数据库之MongoDB查询
如果您不习惯MongoDB查询语言,那么Studio3T的SQL查询可能会派上用场。编写SQL选择表达式和SQL联接来查询mongodb,并查看SQL查询如何转换为MQL——这是一个很好的工具。SQL查询语言
mongodb 常用查询语法包括分页等用法
MongoDB高级查询用法大全
很全面的mongodb 查询语法和实例 $gt $lt $gte $lte $exists $size $type 等等
MongoDB基本查询.pdf 学习资料 复习资料 教学资源
MongoDB命令查询.txt
主要介绍了Thinkphp使用mongodb数据库实现多条件查询方法,修改了mongodb驱动文件来实现复合查询、多条件查询,需要的朋友可以参考下
主要介绍了使用aggregate在MongoDB中查询重复数据记录的方法的相关资料,需要的朋友可以参考下
mongodb查询数据,显示DBCursor.next这种形式,那如何把值带到JSP显示呢? 上传的是一个项目Demo,导入可以直接运行,当然也可以直接查看其中的代码。 项目是使用jquery.ajax显示后台返回的Json串。 若再详细了解...
一个是用Scala编写的MongoDB查询指定域语言 示例代码: val query = Venue where (_.venuename eqs "Starbucks")query.count()query.countDistinct(_.mayor)query.fetch()query.fetch(n)query.get() // equivalent...
真实有效的mongodb视频教程地址 深入浅出 MongoDB 高清IT教程视频下载 1.1、-nosql与MongoDB.mp4 1.2、-MongoDB安装配置.mp4 1.3、-MongoDB?shell详.mp4 1.4、-MongoDB文档、集合、... 2.4、-MongoDB查询语法3.mp4
mongodb查询性能 内容概要:mongodb查询性能 适合人群:工作1-3年的运维人员
MongoDB基本的语法
1、MongoDB查询 4 2、order和投影查询 5 3、MongoDB增加 6 4、MongoDB修改 7 5、MongoDB删除 9 6、MongoDB分页 10 7、MongoDB模糊查询和其他条件查询 12 五、Mongoose(node使用)(前提安装好node环境有node基础) 13...
MongoDB之查询
MongoDB 学习PPT
本课程是一套关于MongoDB应用开发的实战性教程,名为《深入浅出MongoDB应用实战开发(基础、开发指南、系统管理、集群及系统架构)》,教程侧重于讲解MongoDB的常用特性及高级特性,从实际开发的角度出发对MongoDB...
文档详细描述了MongoDB使用find或者findOne时的shell查询语法,对于掌握mongoDB是非常有用的
MongoDB高级查询,许多有用的查询函数,相信大家会用得到的