MongoDB常用操作---查询find方法
(来源:MongoDB常用操作一查询find方法db.collection_name.find())
db.collection_name.find()
-
基本查询
db.users.find({}, {'name' : 1, 'skills' : 1});
where name = 'hurry';
第一个{} 放where条件 第二个{} 指定那些列显示和不显示 (0表示不显示 1表示显示) -
and查询
db.users.find({'name' : 'hurry', 'age' : 18});
where name = 'hurry' and age = 18; -
or查询
db.users.find({ '$or' : [{'name' : 'hurry'}, {'age' : 18}]});
where name = 'hurry' or age = 18; -
比较查询 <, <=, >, >= ($lt, $lte, $gt, $gte )
db.users.find({'age' : {'$gte' : 20, '$lte' : 30}});
where age >= 20 and age <= 30; -
in, not in ($in, $nin)
db.users.find({'age' : {'$in' : [10, 22, 26]}});
where age in (10, 22, 26); -
匹配null
db.users.find({'age' : null);
where age is null; -
like (mongoDB 支持正则表达式)
db.users.find({name:/hurry/});
where name like "%hurry%";
db.users.find({name:/^hurry/});
where name like "hurry%"; -
使用distinct
db.users.distinct('name');
select distinct (name) from users; -
使用count
db.users.find({"price":"1"}).count()
select count(*) from users where price = "1"; -
数组查询(mongo特色)
$all
db.users.find({"price": {'$all' : ['3.1','4.1']}})
查询price是 ['3.1','4.1']$size
db.users.find({"price":{$size:2}})
查询price是 二维数组$slice
db.users.find({},{"price":{$slice :3}})
设置返回的price的长度
假设 price 为 ['3.1','4.1','5.1']
db.users.find({},{"price":{$slice :3}}) 返回 ['3.1','4.1','5.1']
db.users.find({},{"price":{$slice :2}}) 返回 ['3.1','4.1']
db.users.find({},{"price":{$slice :1}}) 返回 ['3.1']可以设置2个整数 第一个是skipNum 第二个是返回的长度
db.users.find({},{"price":{$slice :[1,1]}}) 返回 ['4.1']
如果skip大于等于price的长度,则返回[] -
$where查询
$gt:大于
$lt:小于
$gte:大于或等于
$lte:小于或等于
db.collection.find({ "field" : { $gt: value1, $lt: value2 } } ); // value1 < field < value不等于 $ne
db.things.find( { x : { $ne : 3 } } );in 和 not in ($in $nin)
db.collection.find( { "field" : { $in : array } } );取模运算$mod
db.things.find( "this.a % 10 == 1")
db.things.find( { a : { $mod : [ 10 , 1 ] } } )$exists用来判断一个元素是否存在
db.things.find( { a : { $exists : true } } ); // 如果存在元素a,就返回
db.things.find( { a : { $exists : false } } ); // 如果不存在元素a,就返回$elemMatch
The $elemMatch operator limits the contents of an array field from the query results to contain only the first element matching the $elemMatch condition.
限制返回内容,并且只返回第一个匹配的数据{ _id: 1, zipcode: "63109", students: [ { name: "john", school: 102, age: 10 }, { name: "jess", school: 102, age: 11 }, { name: "jeff", school: 108, age: 15 } ] } { _id: 2, zipcode: "63110", students: [ { name: "ajax", school: 100, age: 7 }, { name: "achilles", school: 100, age: 8 }, ] } { _id: 3, zipcode: "63109", students: [ { name: "ajax", school: 100, age: 7 }, { name: "achilles", school: 100, age: 8 }, ] } { _id: 4, zipcode: "63109", students: [ { name: "barney", school: 102, age: 7 }, { name: "ruth", school: 102, age: 16 }, ] }
查询语句:db.schools.find( { zipcode: "63109" },
{ students: { $elemMatch: { school: 102 } } } )返回结果
{ "_id" : 1, "students" : [ { "name" : "john", "school" : 102, "age" : 10 } ] } { "_id" : 3 } { "_id" : 4, "students" : [ { "name" : "barney", "school" : 102, "age" : 7 } ] }
-
正则表达式
db.things.find({"price":/4.1/})
返回price数组里面有4.1的数据
db.things.find({"productId":/6{1,6}/})
返回productId值为6到666666的数据