MongoDB常用操作 find

MongoDB常用操作  find
一、查询



find方法

db.collection_name.find();



查询所有的结果:

select * from users;

db.users.find();

指定返回那些列(键):

select name, skills from users;

db.users.find({}, {'name' : 1, 'skills' : 1});

补充说明: 第一个{} 放where条件 第二个{} 指定那些列显示和不显示 (0表示不显示 1表示显示)

where条件:

1.简单的等于:

select name, age, skills from users where name = 'hurry';

db.users.find({'name' : 'hurry'},{'name' : 1, 'age' : 1, 'skills' : 1});

2.使用and

select name, age, skills from users where name = 'hurry' and age = 18;

db.users.find({'name' : 'hurry', 'age' : 18},{'name' : 1, 'age' : 1, 'skills' : 1});

3.使用or

select name, age, skills from users where name = 'hurry' or age = 18;

db.users.find({ '$or' : [{'name' : 'hurry'}, {'age' : 18}] },{'name' : 1, 'age' : 1, 'skills' : 1});

4.<, <=, >, >= ($lt, $lte, $gt, $gte )

select * from users where age >= 20 and age <= 30;

db.users.find({'age' : {'$gte' : 20, '$lte' : 30}});

5.使用in, not in ($in, $nin)

select * from users where age in (10, 22, 26);

db.users.find({'age' : {'$in' : [10, 22, 26]}});

6.匹配null

select * from users where age is null;

db.users.find({'age' : null);

7.like (mongoDB 支持正则表达式)

select * from users where name like "%hurry%";

db.users.find({name:/hurry/}); 

select * from users where name like "hurry%";

db.users.find({name:/^hurry/}); 

8.使用distinct

select distinct (name) from users;

db.users.distinct('name');

9.使用count

select count(*) from users;

dunt();



10.数组查询 (mongoDB自己特有的)

如果skills是 ['java','python']

db.users.find({'skills' : 'java'}); 该语句可以匹配成功

$all

db.users.find({'skills' : {'$all' : ['java','python']}}) skills中必须同时包含java 和 python 

$size

db.users.find({'skills' : {'$size' : 2}}) 遗憾的是$size不能与$lt等组合使用

$slice

db.users.find({'skills' : {'$slice : [1,1]}})

两个参数分别是偏移量和返回的数量

11.查询内嵌文档 


12.强大的$where查询
db.foo.find();                   
{ "_id" : ObjectId("4e17ce0ac39f1afe0ba78ce4"), "a" : 1, "b" : 3, "c" : 10 }
{ "_id" : ObjectId("4e17ce13c39f1afe0ba78ce5"), "a" : 1, "b" : 6, "c" : 6 }

如果要查询 b = c 的文档怎么办?

> db.foo.find({"$where":function(){
    for(var current in this){
        for(var other in this){
            if(current != other && this[current] == this[other]){
                return true;    
            }
        }
    }
    return false;

}});



{ "_id" : ObjectId("4e17ce13c39f1afe0ba78ce5"), "a" : 1, "b" : 6, "c" : 6 }



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 : field <= value



如查询j大于3,小于4:


db.things.find({j : {$lt: 3}});
db.things.find({j : {$gte: 4}});



也可以合并在一条语句内:


db.collection.find({ "field" : { $gt: value1, $lt: value2 } } ); // value1 < field < value





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类似,但是他需要匹配条件内所有的值:
如有一个对象:
{ a: [ 1, 2, 3 ] }



下面这个条件是可以匹配的:


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 } } ); // 如果存在元素a,就返回
db.things.find( { a : { $exists : false } } ); // 如果不存在元素a,就返回


8)  $type

$type 基于 bson type来匹配一个元素的类型,像是按照类型ID来匹配,不过我没找到bson类型和id对照表。


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


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)操作符,只能用变通的办法代替,可以参考下面的链接:

+operations+in+query+expressions
分类: MongoDB

转载于:https://my.oschina.net/jinxf/blog/534219

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值