[MongoDB] Query, update, index and group

Source: Internet
Author: User

/*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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.