Saturday 3 June 2017

Error 'Cannot apply $pull to a non-array value'

db.collection(USERDETAILS_COLLECTION).findOneAndUpdate(
                                { "emailVerificationHistory.verifyCode":mailCode },
                                {'$pull':{"emailVerificationHistory.$.verifyCode":mailCode}},
                                function(err,pullData) {
                                    if(err) {
                                        console.log(err);
                                    } else {
                                        res.send({msg: 'User Verified Succesfully', status: 1});
                                    }
                                });

When I execute the above code, I am getting the following error.

name: 'MongoError',
  message: 'Cannot apply $pull to a non-array value',
  ok: 0,
  errmsg: 'Cannot apply $pull to a non-array value',
  code: 16837,

Below is my MongoDB object.

{
    "_id" : ObjectId("593270b8bc98ea235cfaf68f"),
    "username" : "parengalhisham@gmail.com",
    "roleId" : 3,
    "userId" : ObjectId("593270b8bc98ea235cfaf68e"),
    "profile" : {
        "firstName" : "Hisham",
        "lastName" : "Mubarak"
    },
    "activeFlag" : 1,
    "authType" : [ 
        "facebook"
    ],
    "emailVerified" : false,
    "emailVerificationHistory" : [ 
        {
            "email" : "parengalhisham@gmail.com",
            "verifyCode" : "59327155480b791b7845dfd7",
            "requestDate" : ISODate("2017-06-03T08:20:37.156Z"),
            "verifiedDate" : ISODate("2017-06-03T10:31:53.720Z")
        }
    ]
}

How do I search through the emailVerificationHistory array using verifyCode and remove the verifyCode only from that first object inside array? Thank you for your help in advance.



via Hisham Mubarak

No comments:

Post a Comment