猫鼬转换为嵌入式失败

时间:2019-03-31 12:32:07

标签: mongodb mongoose

我的mongodb模式如下

var deviceDataSchema = Schema({
    device_id  : {type:String,index: true, required: true},
    date: {type:Date,  required: true},
    commands:[
      {
        commandId: {type:String},
        status:{type:String},
        creationTime: {type:Date},
        platformIssuedTime: {type:Date},
        issuedTimes: {type:Number,default:0}
      }
    ]
  });


   const query = {device_id: 'a0ea935c-c94d-4bb8-bcd4-92892f309192',
      date: 2019-03-30T16:00:00.000Z};
           let update = {
              "$push": {
                 "commands": {
                   commandId: '672da0371fe3404197bf7be993a3176f',
                   status: 'SENT',
                   creationTime: undefined,
                   platformIssuedTime: '20190331T122144Z',
                   issuedTimes: 0
                 }
              }
           };
   collection.findOneAndUpdate(query,update,{new:true,strict:false ,upsert:true}, function(err,data){});

我的问题是执行代码时,错误弹出,如图所示

CastError:对值“ {commandId:\'672da0371fe3404197bf7be993a3176f \',\ n状态:\'SENT \',\ n creationTime:未定义,\ n platformIssuedTime:\'20190331T122144Z \,\ n :0}“放在路径“命令”

1 个答案:

答案 0 :(得分:1)

我怀疑显示错误的原因是由于字段值creationTime: undefined。在架构设计中,您提到过它是日期类型的数据。

注意:如果不想在数组中重复,可能可以在猫鼬中使用$ addToSet。