Bulk Modify fields in MongoDB

Source: Internet
Author: User

//Add a image_count field for each article document to record the number of images that this article containsdb[' Test.articles '].find ({' title ': ' WFC Test '}). ForEach (function(thisarticle) {//This is going to be forced to reshape, or it will become a floating point    //to use Numberint to convert to shaping, use parseint to get the remaining floating-point numberImagecount = Numberint (thisarticle[' article_images '].length) db[' Test.articles '].update ({' _id ': thisarticle[' _id '}, {$set: {' Image_count '): Imagecount}} )   })//a more streamlined and able-to-do notationDb.test.articles.find ({' title ': ' WFC Test '}). ForEach (function(thisarticle) {Thisarticle.image_count=numberint (thisArticle.article_images.length) Db.test.articles.save (thisarticle)})//After you put the document's fields in a nested array, delete the field (data structure modification)db[' Test.game '].find ({' article_image ': {$exists: 0}}). ForEach (function(thisarticle) {articleimage= {' Remote ': Thisarticle.game_icon, ' local ': Thisarticle.game_local_icon,' Original ': Thisarticle.game_remote_icon, ' upload ': thisarticle.icon_upload} thisarticle.image_upload=thisarticle.icon_upload Thisarticle.modify_time=thisarticle.spider_time thisarticle.article_image=[] ThisArticle.article_image.push (articleimage)DeleteThisarticle.game_iconDeleteThisarticle.game_local_iconDeleteThisarticle.game_remote_iconDeletethisarticle.icon_upload db[' Test.game '].save (thisarticle)})//Group statistics by namedb[' Test.articles '].group ({key:{' Name ': 1}, condition:{' name ': {$exists: 1}}, $reduce:function(Curr, result) {Result.total+ = 1}, Initial:{total:0},})

Bulk Modify fields in MongoDB

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.