$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