desert3 发表于 2013-1-30 01:53:33

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 commentsdb.posts.find({}, {comments:{$slice: -5}}) // last 5 commentsdb.posts.find({}, {comments:{$slice: }}) // skip 20, limit 10db.posts.find({}, {comments:{$slice: [-20, 10]}}) // 20 from end, limit 103,比较操作符($gt,$lt,$gte,$lte,$ne)db.collection.find({ "field" : { $gt: value } } );   // greater than: field > valuedb.collection.find({ "field" : { $lt: value } } );   // less than:field < valuedb.collection.find({ "field" : { $gte: value } } );// greater than or equal to : field >= valuedb.collection.find({ "field" : { $lte: value } } );// less than or equal to : field <= valuedb.things.find( { x : { $ne : 3 } } );               // not equals : field != valuedb.collection.find({ "field" : { $gt: value1, $lt: value2 } } );    // value1 < field < value4,必须包含$all数组可以比$all操作符对应的数据含有更加多的元素,$all表示满足条件元素的最小集合{ a: [ 1, 2, 3 ] }db.things.find( { a: { $all: [ 2, 3 ] } } ); // matchdb.things.find( { a: { $all: [ 2, 3, 4 ] } } ); // not match5,是否存在$existsdb.things.find( { a : { $exists : true } } ); // return object if a is presentdb.things.find( { a : { $exists : false } } ); // return if a is missing6,取模运算符$moddb.things.find( "this.a % 10 == 1");db.things.find( { a : { $mod : [ 10 , 1 ] } } ) ; //better7,IN,NOTIN运算符$in,$nindb.collection.find( { "field" : { $in : array } } );db.things.find({j:{$in: }});db.things.find({j:{$nin: }});8,OR,NOT OR, NOT运算符$or,$nor,$notdb.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元素类型判断运算符$typedb.things.find( { a : { $type : 2 } } ); // matches if a is a stringdb.things.find( { a : { $type : 16 } } ); // matches if a is an intType Name Type NumberDouble 1String 2Object 3Array 4Binary data 5Object id 7Boolean 8Date 9Null 10Regular expression 11JavaScript code 13Symbol 14JavaScript code with scope 1532-bit integer 16Timestamp 1764-bit integer 18Min key 255Max 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,遍历数组$elemMatchUse $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 memorycount方法默认忽略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 order19,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:{}});
页: [1]
查看完整版本: MongoDB常用数据库操作