MongoDB Array Manipulation

Source: Internet
Author: User

$push adds an element to the document array and automatically adds the array if there is no array.

Db.foo.update ({},{"$push": {"Users": 1}}) adds a 1 element to the end of the users array

Db.foo.update ({},{"$push": {"users": {"$each": []}}) adds multiple elements to the end of the users array, to $each with

$addToSet can avoid inserting duplicate elements, with $each, you can add multiple different values, and the array modifier is slow because the document size may change

$pop remove elements from the head or tail

$pull Delete the element that satisfies the condition, not just delete a

By default, updates can only be performed on the first document that matches the matching criteria, and if multiple documents meet the criteria, only the first document is updated and the other documents are not changed.

To update all matching documents, you can set the fourth parameter of update to True

Db.foo.update ({},{"$set": {name: ' tes '}},false,true)

MongoDB Array Manipulation

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.