MongoDB常用数据库操作

function find(query, fields, limit, skip)
1,多个where条件
// i.e., select * from things where x=3 and y="foo"
db.things.find( { x : 3, y : "foo" } );

db.things.find({j: {$ne: 3}, k: {$gt: 10} });

2,查询部分字段
// select z from things where x="john"(查询字段z)
db.things.find( { x : "john" }, { z : 1 } );

// get all posts about 'tennis' but without the comments field(查询除了comments以外的所有字段)
db.posts.find( { tags : 'tennis' }, { comments : 0 } );

默认查询出来的结果包含_id字段,如果不想要,你可以排除他
db.things.find( { x : "john" }, { z : 1 , _id : 0} );

// 查询子文档的部分字段
t.find({})
"_id" : ObjectId("4c23f0486dad1c3a68457d20"), "x" : { "y" : 1, "z" : [ 1, 2, 3 ] } }
t.find({}, {'x.y':1})
"_id" : ObjectId("4c23f0486dad1c3a68457d20"), "x" : { "y" : 1 } }

// 数组截取
db.posts.find({}, {comments:{$slice: 5}}) // first 5 comments
db.posts.find({}, {comments:{$slice: -5}}) // last 5 comments
db.posts.find({}, {comments:{$slice: [20, 10]}}) // skip 20, limit 10
db.posts.find({}, {comments:{$slice: [-20, 10]}}) // 20 from end, limit 10

3,比较操作符($gt,$lt,$gte,$lte,$ne)
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
db.things.find( { x : { $ne : 3 } } );               // not equals : field != value

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

4,必须包含$all
数组可以比$all操作符对应的数据含有更加多的元素,$all表示满足条件元素的最小集合
{ a: [ 1, 2, 3 ] }
db.things.find( { a: { $all: [ 2, 3 ] } } ); // match
db.things.find( { a: { $all: [ 2, 3, 4 ] } } ); // not match

5,是否存在$exists
db.things.find( { a : { $exists : true } } ); // return object if a is present
db.things.find( { a : { $exists : false } } ); // return if a is missing

6,取模运算符$mod
db.things.find( "this.a % 10 == 1");
db.things.find( { a : { $mod : [ 10 , 1 ] } } ) ; //better

7,IN,NOTIN运算符$in,$nin
db.collection.find( { "field" : { $in : array } } );
db.things.find({j:{$in: [2,4,6]}});

db.things.find({j:{$nin: [2,4,6]}});

8,OR,NOT OR, NOT运算符$or,$nor,$not
db.foo.find( { $or : [ { a : 1 } , { b : 2 } ] } )
db.foo.find( { name : "bob" , $or : [ { a : 1 } , { b : 2 } ] } )

db.customers.find( { name : { $not : /acme.*corp/i } } );
db.things.find( { a : { $not : { $mod : [ 10 , 1 ] } } } );

9,数组元素个数运算符$size
$size不支持范围操作,只能是size = 操作
db.things.find( { a : { $size: 1 } } );

10,BSON元素类型判断运算符$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 

11,正则表达式
db.customers.find( { name : /acme.*corp/i } );

12,值是否在数组,值是否在嵌套对象
db.things.find( { colors : "red" } );  // To look for the value "red" in an array field colors:
db.postings.find( { "author.name" : "joe" } ); // For example, to look author.name=="joe" in a postings collection with embedded author objects:

13,遍历数组$elemMatch
Use $elemMatch to check if an element in an array matches the specified match expression.
t.find( { x : { $elemMatch : { a : 1, b : { $gt : 1 } } } } )

{ "_id" : ObjectId("4b5783300334000000000aa9"),
  "x" : [ { "a" : 1, "b" : 3 }, 7, { "b" : 99 }, { "a" : 11 } ]
}

t.find( { "x.a" : 1, "x.b" : { $gt : 1 } } )

14,JavaScript表达式和$where
下面的4个表达式等价!!JavaScript表达式的执行效率比使用上述操作符的执行时间差些,不过使用起来更加灵活些
db.myCollection.find( { a : { $gt: 3 } } );
db.myCollection.find( { $where: "this.a > 3" } );
db.myCollection.find("this.a > 3");
f = function() { return this.a > 3; } db.myCollection.find(f);

15,count函数
对查询结果计数,在MongoDB Server端计数比MongoDB Client端更加快速和有效
nstudents = db.students.find({'address.state' : 'CA'}).count();
nstudents = db.students.find({'address.state' : 'CA'}).toArray().length; // VERY BAD: slow and uses excess memory

count方法默认忽略count前面使用的skip,limit函数,可以使用count(true)来使得skip,limit起作用
n = db.students.find().skip(20).limit(10).count(true);

16,limit函数
db.students.find().limit(10).forEach( function(student) { print(student.name + "<p>"); } );

17,skip函数
function printStudents(pageNumber, nPerPage) {
   print("Page: " + pageNumber);
   db.students.find().skip((pageNumber-1)*nPerPage).limit(nPerPage).forEach( function(student) { print(student.name + "<p>"); } );
}

18,sort函数
db.myCollection.find().sort( { ts : -1 } ); // sort by ts, descending order

19,min,max函数(查询条件)
db.f.find().min({name:"barry"}}.max({name:"larry"}).hint({name:1}); // 包含小的,不包含大的
db.f.find().min({name:"barry"}}.max({name:"larry"});
db.f.find().min({last_name:"smith",first_name:"john"}};

db.f.find({$min: {name:"barry"}, $max: {name:"larry"}, $query:{}});
 

猜你喜欢

转载自hz-chenwenbiao-rr.iteye.com/blog/1616966
今日推荐