jsky
jsky

Reputation: 2217

How to correctly update embedded object's field in mongodb?

How do i correctly update the resetPasswordExpires field in mongodb document:

{ 
    "_id" : ObjectId("000"), 
    "username" : "root", 
    "isActive" : "yes", 
    "email" : "[email protected]", 
    "roles" : { 
        "admin" : ObjectId("111") 
    }, 
    "resetPasswordExpires" : ISODate("2015-06-19T18:04:40.014Z"),
    "resetPasswordToken" : "token" 
}

I tried:

db.users.update(
    { item: "000" },
    {
      $set: {  "roles.resetPasswordExpires":  ISODate("2015-06-20T18:04:40.014Z")}
})

and

db.users.update(
    { item: "111" },
    {
      $set: {  "roles.resetPasswordExpires":  ISODate("2015-06-20T18:04:40.014Z")}
})

and a few other variations without matching.

Upvotes: 0

Views: 47

Answers (1)

Jason Cust
Jason Cust

Reputation: 10899

The query needs to match on the _id (at least that is what it appears you are trying to match on). The update value for resetPasswordExpires is not a property of roles. Something like the following should work:

db.users.update(
  { _id: ObjectId("000") },
  {$set: {  "resetPasswordExpires":  ISODate("2015-06-20T18:04:40.014Z")}
});

Upvotes: 1

Related Questions