MongoDB- Insert if it doesn't exist, else skip

后端 未结 3 786
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-30 20:59

Is it possible to insert in Mongo with condition;

//Pseudo code

Bulk Insert Item :

If Key exists
    Skip, don\'t throw error
If key does not exist
    Add         


        
3条回答
  •  遥遥无期
    2020-12-30 21:54

    Use setOnInsert

    db.collection('collection').updateOne(
         { _id: data._id },
        { $setOnInsert: { ...data } },
        { upsert: true },
      )
    

提交回复
热议问题