MongoDB Insert/update/one2many Case

Source: Internet
Author: User
Tags bulk insert

Blog posts and comments For example, the blog post has a title content, corresponding to a number of comments, comments have comments, comments and other content. (1) Insert a blog post: Db.blog.insert ({'_id':' One','title':'This is blog Title1','content':'This is blog Content1'})(2) Update a blog post db.blog.update ({'_id':' One'}, {$set: {'title':'This is blog title2','content':'This is blog Content2'}})(3) to update a blog post, insert db.blog.update if it does not exist ({'_id':' A'}, {$set: {'title':'This is blog title4','content':'This is blog content4'}}, {upsert:true})(4Add a comment to the blog post db.blog.update ({'_id':' One'}, {$push: {'Comments':{'User':'User1','content':'Reviews 1'}}})(5Delete the Post's comment db.blog.update based on the criteria ({'_id':' One'}, {$pull: {'Comments':{'User':'User1'}}})(6use $addtoset to avoid adding duplicate data db.blog.update ({'_id':' One'}, {$addToSet: {'Comments':{'User':'User1','content':'Reviews 1'}}})(7) with $addtoset &BULK INSERT Data db.blog.update $each Union operation ({'_id':' One'}, {$addToSet: {'Comments':{'$each':[    {'User':'User1','content':'Reviews 1'},    {'User':'User2','content':'Reviews 2'},    {'User':'User3','content':'Reviews 3'},   ]}}})

MongoDB Insert/update/one2many Case

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.