【问题标题】:mongoose Cast to embedded failed猫鼬投射到嵌入式失败
【发布时间】:2019-03-31 20:51:15
【问题描述】:

我的 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: Cast to embedded failed for value "{ commandId: \'672da0371fe3404197bf7be993a3176f\',\n status: \'SENT\',\n creationTime: undefined,\n platformIssuedTime: \'20190331T122144Z\',\n mappedTimes : 0 }" 在路径“命令”

【问题讨论】:

    标签: mongodb mongoose


    【解决方案1】:

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

    注意:如果您不希望数组中有重复项,您可能可以在 mongoose 中使用 $addToSet。

    【讨论】:

      猜你喜欢
      • 2019-05-03
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      • 2014-08-03
      • 2016-05-27
      • 2017-01-16
      • 1970-01-01
      相关资源
      最近更新 更多