猫鼬ups而不删除现有字段

时间:2018-11-07 08:52:17

标签: node.js mongodb mongoose

我的代码的目标是将一个对象插入数据库。我创建的架构有点复杂

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const objectSchema = new Schema(
    { message: String },
    { discriminatorKey: "type", _id: false }
);

const cameraSchema = new Schema(
    {
        number: { type: Number, required: true },
        name: { type: String, required: true } //Do not update if exists
    },
    { _id: false }
);

const mainSchema = new Schema(
    {
        name: { type: String, required: true, trim: true },
        lastUpdateTime: { type: Date, default: Date.now },
        objects: [objectSchema]
    },
    { collection: "mainCollection" }
);

mainSchema.path("objects").discriminator(
    "TypeA",
    new Schema(
        {
            cameras: [cameraSchema],
            enabled: { type: Boolean } //Do not update if exists
        },
        { _id: false }
    )
);

mainSchema.path("objects").discriminator(
    "TypeB",
    new Schema(
        {
            available: { type: Boolean, required: true },
            enabled: { type: Boolean } //Do not update if exists
        },
        { _id: false }
    )
);

module.exports = mongoose.model("mainModel", mainSchema);

我正在从外部来源接收以下对象

{
  "name": "040C7",
  "objects": [
    {
      "type": "TypeA",
      "cameras": [
        {
          "number": 1
        },
        {
          "number": 2
        },
        {
          "number": 3
        }
      ]
    },
    {
      "type": "TypeB",
      "available": false
    }
  ]
}

如果该名称存在于数据库中,则应更新收到的每个字段,而不必删除/更新字段:

    TypeA TypeB 中的
  • enabled 相机对象中的
  • name

否则,我应该将其插入数据库中,其中包含enabled(真)和name(相机)的所有字段和默认值


我已经尝试过UpdateOne()函数,但是当我的字段enabledname已经在我的数据库中时,它将删除它们

const obj = new myModel(message);

let upsertObj = obj.toObject();

delete upsertRcm._id;

MyModel.updateOne(
    { name: obj.name },
    upsertObj,
    { upsert: true },
    err => {
        console.log(err)
    }
);

我该如何说猫鼬来保留数据库中已有但我接收的对象中没有的字段?

数据库中的文档

{
  "lastUpdateTime": "2018-11-07T09:12:30.750Z",
  "name": "040C7",
  "objects": [
    {
      "type": "TypeA",
      "enabled": true,
      "cameras": [
        {
          "number": 1,
          "name": "Camera",
        }
      ]
    },
    {
      "type": "TypeB",
      "enabled": true,
      "available": false
    }
  ]
}

在更新接收到的对象之后

{
  "lastUpdateTime": "2018-11-07T09:14:30.600Z",
  "name": "040C7",
  "objects": [
    {
      "type": "TypeA",
      "cameras": [
        {
          "number": 1,
        },
        {
          "number": 2,
        },
        {
          "number": 3,
        }
      ]
    },
    {
      "type": "TypeB",
      "available": false
    }
  ]
}

但是应该是

{
  "lastUpdateTime": "2018-11-07T09:14:30.600Z",
  "name": "040C7",
  "objects": [
    {
      "type": "TypeA",
      "enabled": true
      "cameras": [
        {
          "number": 1,
          "name": "Camera"
        },
        {
          "number": 2,
        },
        {
          "number": 3,
        }
      ]
    },
    {
      "type": "TypeB",
      "enabled": true,
      "available": false
    }
  ]
}

MongoDB版本:4.0.2

name字段是我的唯一标识符。数组中将仅出现一种类型“ TypeA”,“ TypeB”,...。我永远不会在我的对象中收到2个“ TypeA”

必须更新的字段列表:

  • objects["TypeA"].cameras.number(新对象的值)
  • objects["TypeB"].available(新对象的值)
  • lastUpdateTime(现在日期)

数据库中应保留的字段列表:

  • objects["TypeA"].enabled
  • objects["TypeA"].cameras.name
  • objects["TypeB"].enabled

0 个答案:

没有答案