考虑架构:

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中并且它们的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文档之一,将其复制并使用TotalClaimsToBeClaimedByClientType设置其howManyClaims属性。

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

最佳答案

根据您的查询,由于您正在执行.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在数据库中不存在,其余的125TotalClaimsToBeClaimedByClientType已更新。

参考:bulkWrite

10-04 17:20