通过字段的当前值更新MongoDB中的多个文档

时间:2019-11-18 10:37:21

标签: javascript node.js mongodb mongoose

我有一个员工架构:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const EmployeesSchema = new Schema({

  EmployeeId: {
    type: String,
    required: true
  },


  // .... more attributes


  Copies: {
    type: Number,
    required: true

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

module.exports = Employees = mongoose.model(
  "employees",
  EmployeesSchema
);

和id数组...

const _ids = // I get this one from some code manipulation - return the `_id` attribute

我想通过一个属性-_ids更新Copies中的那些文档:

await Employees.update(
    {
      _id: {
        $in: _ids
      }
    },
    {
      $set: { Copies: Copies - 1 }  // here it looks for a var called Copies
    },
    { multi: true }
  );

如何根据每个文档的1值从Copies属性中减少Copies

0 个答案:

没有答案