如果存在,则更新许多,否则为每个不存在的LeadId创建一个新文档

时间:2019-12-27 23:21:26

标签: javascript node.js mongodb mongoose

考虑架构:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const EightWeekGamePlanSchema = new Schema({
  Week: {
    type: Number,
    required: true
  },
  LeadId: {
    type: String,
    required: true
  },
  PackageId: {
    type: String,
    required: true
  },
  BusinessName: {
    type: String,
    required: true
  },
  PhoneNumberMasque: {
    type: String,
    required: true
  },
  City: {
    type: String,
    required: true
  },
  Rooms: {
    type: Number,
    required: true
  },
  LeadStartDate: {
    type: Date
  },
  LeadEndDate: {
    type: Date
  },

  TargetedToBeClaimedByClientType: {
    type: Number,
    required: true
  },
  TotalClaimsLeftToBeClaimedByClientType: {
    // incresed by 1 every time it's claimed
    type: Number,
    required: true
  },
  TotalClaimsToBeClaimedByClientType: {
    // Stays fixed
    type: Number,
    required: true
  },
  Status: {
    type: Number,
    required: true
  },

  InsertDate: {
    type: Date,
    default: Date.now
  }
});

module.exports = EightWeekGamePlan = mongoose.model(
  "eightweekgameplan",
  EightWeekGamePlanSchema
);

我正在尝试编写一个非常复杂的查询:如果howManyClaims出现在给定数组LeadID和{{1}中,则按winnerLeads中的给定编号更新多个文档}属性等于给定的TargetedToBeClaimedByClientType

groupTarget

否则,执行某种router.post("/add-claims-to-group", auth, async (req, res) => { const { howManyClaims, groupTarget, winnerLeads, week } = req.body; EightWeekGamePlan.updateMany( { LeadId: { $in: winnerLeads }, TargetedToBeClaimedByClientType: groupTarget }, { $inc: { TotalClaimsToBeClaimedByClientType: howManyClaims } }, { multi: true }, (err, writeResult) => { if (err) { console.log(err); } else { console.log(writeResult); } } ); } :取属于Create的现有EightWeekGamePlan文档之一,复制它,并用{设置其LeadID属性{1}}。

是否可以在一个查询中结合使用Update和Create动作?

1 个答案:

答案 0 :(得分:1)

根据您的查询,由于您正在执行.updateMany(),因此不必执行{ multi: true }。无论如何,通常您都可以使用{upset: true}进行upsert,但是理想情况下,只有在数据库中找不到匹配项的情况下,它才会根据过滤条件以及输入查询中的更新字段来创建新文档。但是,由于我们在过滤条件中有一个列表($in),因此可能无法正常工作,请尝试以下方法:

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);

收集数据:

/* 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
}

结果:

/* 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
}

基本上,bulkWrite除了写入结果外,不返回任何文档,您可以在DB中验证更新操作结果,此外,根据以上结果,6插入为LeadId : 3 + TargetedToBeClaimedByClientType" : 1.0(因此,LeadId:3 (重复))组合在数据库中不存在,并且由于7在数据库中不存在而被插入LeadId : 31,其余125的{ {1}}已更新。

参考: bulkWrite