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

后端 未结 5 1696
滥情空心
滥情空心 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:39

    If you want to update multiple records, it's important to pass true as the 4th argument to the update function:

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

    Per the MongoDB shell syntax for update():

    db.collection.update( criteria, objNew, upsert, multi )
    
    • upsert - if the record(s) do not exist, insert one. Upsert only inserts a single document
    • mutli - indicates if all documents matching criteria should be updated rather than just one

提交回复
热议问题