user824624
user824624

Reputation: 8068

mongoose Cast to embedded failed

My mongodb schema is as followed

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){});

my problem is when the code is executed, the error pops out as shown

CastError: Cast to embedded failed for value "{ commandId: \'672da0371fe3404197bf7be993a3176f\',\n status: \'SENT\',\n creationTime: undefined,\n platformIssuedTime: \'20190331T122144Z\',\n issuedTimes: 0 }" at path "commands"

Upvotes: 0

Views: 166

Answers (1)

Jayabal Rajendran
Jayabal Rajendran

Reputation: 68

I suspect the reason the error is shown is because of the field value creationTime: undefined. In schema design, you have mentioned it was a date type data.

Note: Probably you could use $addToSet in mongoose if you don't want duplicates in the array.

Upvotes: 2

Related Questions