/*1. Query Operators*/Db.posts.find ({viewscount: {$get:$lte: 3000}}, {_id:0, Viewscount:1, title:1})//$inDb.posts.find ({categories: {$inch: [' iOS ']}}, {categories:1})//$whereDb.posts.find ({$where:function(){ return This. categories.length>15}}, {categories:1, Title:1})//See how many categories from previous query:Db.posts.find ({$where:function(){ return This. categories.length>15}}, {categories:1, title:1}) [0].categories.length
Update:
/* 2. Update*/var d = db.posts.findOne (); // Change the author email:d.author.email = "[email protected]"//Update the database Db.post.update ({_id:d._id}, d); // Update Mutli-document True}}, {multi:true//Add new key, set Mutli
Performance and Indexes:
/*3. Performance and Indexes*///Create the DataDb.numbers.drop ();varnum = 5000000, I= 0; for(i = 0; i < num; i + +)){ varRandomnumber = Math.floor (Math.random () *10000); Db.numbers.insert ({number:randomnumber}); //print (i);}db.number.find ({number: {$gt:$LT: +}, {_id:0, number:1}}). explain ();//It scan all the doucment, took 3500ms //Add Index:Db.numbers.ensureIndex ({number:1});//took 1046 MS, scan 1m instead of 5m
Group:
/* 4. $group*//sum up the Viewcount, rename the result as ' Totalviewscount ' Db.posts.aggregate ({ $group: { _id:null, ' $viewsCount '}} }) //adding 1 for each documentdb.posts.aggregate ({ $group: { _id: NULL , 1}} )// =Db.posts.count ()
[MongoDB] Query, update, index and group