How to stop insertion of Duplicate documents in a mongodb collection

后端 未结 4 1838
孤城傲影
孤城傲影 2020-11-30 01:26

Let us have a MongoDB collection which has three docs..

db.collection.find()

 { _id:\'...\', user: \'A\', title: \'Physics\',   Bank: \'         


        
4条回答
  •  离开以前
    2020-11-30 02:06

    Maybe this is a bit slower than other ways but it works too. It can be used inside a loop:

    db.collection.replaceOne(query, data, {upsert: true})
    

    The query may be something like:

    { _id: '5f915390950f276680720b57' }
    

    https://docs.mongodb.com/manual/reference/method/db.collection.replaceOne

提交回复
热议问题