mongodb的高级查询


db.coll.find({j: {$ne: 3}, k: {$gt: 10} });              select * from coll where j <> 3 and k > 10

===============================================================================

$lt       <                 $ne         <>   !=

$gte   >=          $lte     <=         $gt    > 

=========================================================================

$all   $in

{ a: [ 1, 2, 3 ] }

db.coll.find( { a: { $all: [ 2, 3 ] } } );       符合

db.coll.find( { a: { $all: [ 3, 4 ] } } );      不符合

db.coll.find( { a: { $in : [ 3, 4 ] } } );       符合

=======================================================================================

$exists

db.coll.find( { a : { $exists : true } } );                   // return object if a is present,,,    如果a字段存在就返回
db.coll.find( { a : { $exists : false } } );                 // return if a is missing                  如果a字段不存在就返回该条query

=================================================================================================

$mod

db.coll.find( { a : { $mod : [ 10 , 1 ] } } );            //   select *  from coll  where  a%10  == 1  ;

======================================================================================

$in    $nin

======================================

$or    $nor      or只要有一个符合就行       nor后面的只要有一个就不符合

db.foo.find( { $or : [ { a : 1 } , { b : 2 } ] } )

db.foo.find( { name : "bob" , $or : [ { a : 1 } , { b : 2 } ] } )

========================================================================================

$and

db.foo.insert( { a: [ 1, 10 ] } )
db.foo.find( { $and: [ { a: 1 }, { a: { $gt: 5 } } ] } )

===================================================================

$size

db.things.find( { a : { $size: 1 } } );                   a集合长度为1的项

=======================================================================

$type

db.things.find( { a : { $type : 2 } } ); // matches if a is a string
db.things.find( { a : { $type : 16 } } ); // matches if a is an int


Type Name Type Number
Double 1
String 2
Object 3
Array 4
Binary data 5
Object id 7
Boolean 8
Date 9
Null 10
Regular expression 11
JavaScript code 13
Symbol 14
JavaScript code with scope 15
32-bit integer 16
Timestamp 17
64-bit integer 18
Min key 255
Max key 127

=================================================================================

Regular Expressions  正则匹配

db.customers.find( { name : /acme.*corp/i } );
db.customers.find( { name : { $regex : 'acme.*corp', $options: 'i' } } );

db.customers.find( { name : { $regex : /acme.*corp/i, $nin : ['acmeblahcorp'] } } );

========================================================================================================

$elemMatch

t.find( { x : { $elemMatch : { a : 1, b : { $gt : 1 } } } } )
{   "_id" : ObjectId("4b5783300334000000000aa9"),   "x" : [ { "a" : 1, "b" : 3 }, 7, { "b" : 99 }, { "a" : 11 } ]  }

==========================================

$not

db.customers.find( { name : { $not : /acme.*corp/i } } );

db.things.find( { a : { $not : { $mod : [ 10 , 1 ] } } } );

db.things.find( { a : { $not : true } } ); // syntax error

=========================================================

$where

db.myCollection.find  ( {  registered : true ,  $where : "this.a>3" } )


=============================================================

sql 语句 和 mongo 语句 对比 http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart

如果只需要query出其中的一两个field,而不是整个 BSON 查询出来 。

SELECT a,b FROM users 
db.users.find({}, {a:1,b:1})

SELECT a,b FROM users WHERE age=33
db.users.find({age:33}, {a:1,b:1})

SELECT * FROM users WHERE name LIKE "%Joe%" 
db.users.find({name:/Joe/})

SELECT * FROM users WHERE name LIKE "Joe%"
db.users.find({name:/^Joe/})

SELECT * FROM users WHERE a=1 or b=2
db.users.find( { $or : [ { a : 1 } , { b : 2 } ] } )

























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值