MongoDB Learning Notes $push, $each, $slice combination of Use

Source: Internet
Author: User
Tags array length

$push use:

Add 2 new members to an array using $push

Db. Arraytest.updateone ({"Name": "Bill"},{$push: {"Address": {$each: [{"Street": "Xuhui"},{"Street": "Xingzhuang", "num" : 1}]}})

View results: New data has been added.

$slice is in $push to limit the total length of the array, 1 description array length is 1,-5 description array length is 5, array is 0 description array is empty.

Suppose there are 3 elements in the original address array:

Db. Arraytest.updateone ({"Name": "Bill"},{$push: {"Address": {$each: [{"Street": "Xuhui7"},{"Street": "Xingzhuang7", "num ": 2}], $slice:-3}})

Run the result, we analyze should be the total length of 3, because to add 2 elements, so the first two elements will be deleted, after the Xingzhuang6 will add 2 new elements, the result is we think so?

Yes, sure enough!

MongoDB Learning Notes $push, $each, $slice combination of Use

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.