MongoDB Update (Insert a List of Item to an Array)

后端 未结 5 1716
滥情空心
滥情空心 2021-01-02 16:12

I want to add several items to arrays of several rows in Mongo. How can I do this?

I want to start with this:

{\'x\': \'h\', arr: [1,2,3] }
{\'x\':          


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-02 16:47

    If you have a MongoDB collection named yourCollection and a record with the name x, you would update the subarray with something like this:

    db.test.update( {"name":"x"}, {"$pushAll" : {arr : [1, 2, 3]}} )

    The important keyword here is $pushAll. You can use it to add items to arrays inside of a single record attribute.

提交回复
热议问题