MongoDB查询的语法
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 } } ); // greater than : field > value
db.collection.find({ "field" : { $lt: value } } ); // less than : field < value
db.collection.find({ "field" : { $gte: value } } ); // greater than or equal to : field >= value
db.collection.find({ "field" : { $lte: value } } ); // less than or equal to : fieldt.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
页:
[1]