How to update and upsert multiple documents in MongoDB using C# Drivers

后端 未结 6 1891
清酒与你
清酒与你 2020-12-10 12:37

I am using MongoDB 2, and I want to update multiple documents and upsert a value like processed:true into the collection. But MongoDB c# api only allows us to

6条回答
  •  孤街浪徒
    2020-12-10 13:23

    Try first removing all items to be inserted from the collection, and then calling insert:

            var search = [];
            arrayToInsert.forEach(function(v, k) {
                search.push(v.hash); // my unique key is hash. you could use _id or whatever
            })
            collection.remove({
                'hash' : {
                    $in : search
                }
            }, function(e, docs) {
    
                collection.insert(arrayToInsert, function(e, docs) {
                    if (e) {
                        console.log("data failed to update ", e);
                    }
                    else {
                        console.log("data updated ");
                    }
                });
            })
    

提交回复
热议问题