Skip to content Skip to sidebar Skip to footer

Update Many If Exists , Otherwise Create For Each LeadId That Doesn't Exists A New Document

Consider the Schema : const mongoose = require('mongoose'); const Schema = mongoose.Schema; const EightWeekGamePlanSchema = new Schema({ Week: { type: Number, required:

Solution 1:

From your query, since you're doing .updateMany(), you don't have to do { multi: true }. Anyway usually you can do upsert using {upset: true}, but it would ideally create a new document based on filter criteria with update fields from input query only if no match is found in DB. But since here we're have a list ($in) in filter criteria it might not work normally, try this :

let winnerLeads = [1, 2, 3, 31, 5]
let groupTarget = 1
let howManyClaims = 2
let bulkArr = []
for (i of winnerLeads) {
    bulkArr.push({
        updateOne: {
            "filter": {
                LeadId: i,
                TargetedToBeClaimedByClientType: groupTarget
            },
            // If you wanted it to be incremented rather than replace the field, then try `$inc` instead of `$set`.
            "update": { $set: { TotalClaimsToBeClaimedByClientType: howManyClaims } },
            "upsert": true
        }
    })
}
db.EightWeekGamePlan.bulkWrite(bulkArr);

Collection Data :

/* 1 */
{
    "_id" : ObjectId("5e06eb8f400289966e00fac2"),
    "LeadId" : 1,
    "TotalClaimsToBeClaimedByClientType" : 1.0,
    "TargetedToBeClaimedByClientType" : 1
}

/* 2 */
{
    "_id" : ObjectId("5e06eb98400289966e00fb88"),
    "LeadId" : 2,
    "TotalClaimsToBeClaimedByClientType" : 1.0,
    "TargetedToBeClaimedByClientType" : 1
}

/* 3 */
{
    "_id" : ObjectId("5e06eba0400289966e00fc47"),
    "LeadId" : 3,
    "TotalClaimsToBeClaimedByClientType" : 0,
    "TargetedToBeClaimedByClientType" : 11
}

/* 4 */
{
    "_id" : ObjectId("5e06ebac400289966e00fd4b"),
    "LeadId" : 4,
    "TotalClaimsToBeClaimedByClientType" : 1,
    "TargetedToBeClaimedByClientType" : 11
}

/* 5 */
{
    "_id" : ObjectId("5e06ecef400289966e01273a"),
    "LeadId" : 5,
    "TotalClaimsToBeClaimedByClientType" : 1.0,
    "TargetedToBeClaimedByClientType" : 1
}

Result :

/* 1 */
{
    "_id" : ObjectId("5e06eb8f400289966e00fac2"),
    "LeadId" : 1,
    "TotalClaimsToBeClaimedByClientType" : 2.0,
    "TargetedToBeClaimedByClientType" : 1
}

/* 2 */
{
    "_id" : ObjectId("5e06eb98400289966e00fb88"),
    "LeadId" : 2,
    "TotalClaimsToBeClaimedByClientType" : 2.0,
    "TargetedToBeClaimedByClientType" : 1
}

/* 3 */
{
    "_id" : ObjectId("5e06eba0400289966e00fc47"),
    "LeadId" : 3,
    "TotalClaimsToBeClaimedByClientType" : 0,
    "TargetedToBeClaimedByClientType" : 11
}

/* 4 */
{
    "_id" : ObjectId("5e06ebac400289966e00fd4b"),
    "LeadId" : 4,
    "TotalClaimsToBeClaimedByClientType" : 1,
    "TargetedToBeClaimedByClientType" : 11
}

/* 5 */
{
    "_id" : ObjectId("5e06ecef400289966e01273a"),
    "LeadId" : 5,
    "TotalClaimsToBeClaimedByClientType" : 2,
    "TargetedToBeClaimedByClientType" : 1
}

/* 6 */
{
    "_id" : ObjectId("5e071eb1400289966e0597a0"),
    "TargetedToBeClaimedByClientType" : 1.0,
    "LeadId" : 3.0,
    "TotalClaimsToBeClaimedByClientType" : 2.0
}

/* 7 */
{
    "_id" : ObjectId("5e071e62400289966e059168"),
    "TargetedToBeClaimedByClientType" : 1.0,
    "LeadId" : 31.0,
    "TotalClaimsToBeClaimedByClientType" : 2.0
}

Basically bulkWrite doesn't return any documents except write result, you can verify in DB for update operation result, Also from the above result 6 got insert as LeadId : 3 + TargetedToBeClaimedByClientType" : 1.0(So LeadId:3 is duplicated) combination is not present in DB & 7 got inserted as LeadId : 31 is not present in DB, Remaining 1,2,5's TotalClaimsToBeClaimedByClientType got updated.

Ref : bulkWrite


Post a Comment for "Update Many If Exists , Otherwise Create For Each LeadId That Doesn't Exists A New Document"