MongoDB Replace specific array values

后端 未结 2 909
花落未央
花落未央 2021-01-07 23:09

In MongoDB, I have a movie collection that has an array of languages , e.g.
languages: [0:USA, 1: German, 2: French, ...etc]

The array values are no

2条回答
  •  忘掉有多难
    2021-01-07 23:51

    Use the positional $ operator which identifies the element in the languages array to update without explicitly specifying its position in the array i.e. instead of knowing the position in advance and updating the element as:

    db.movies.updateMany(
        { "languages": "French" }, 
        { "$set": { "languages.2": "Francais" } }
    )
    

    you can just use the $ operator as:

    db.movies.updateMany(
        { "languages": "French" }, 
        { "$set": { "languages.$": "Francais" } }
    )
    

提交回复
热议问题