1

I'm using Node, MongoDB, and Mongoose. I've checked the MongoDB docs on set and update. I've Googled for a little over an hour now and I've found topics that dance around my issue, nothing definitive though. The query below updates the correct field, but it also completely removes the other fields in the array I haven't specified.

db.posts.update( 
  { "_id" : { $exists : true }  },
  { $set : { userCreated : { "time" : new ISODate("2013-07-11T03:34:54Z") } } },
  false,
  true
)

This is the section of the schema I'm trying to modify:

},
userCreated: {
  id: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
  name: { type: String, default: '' },
  time: { type: Date, default: Date.now }
},

This is what comes out:

},
"userCreated": {
  "time": { ISODate("2013-07-11T03:34:54Z") }
},

1 Answer 1

3

You can update the time property of userCreated and leave the other properties alone by using dot notation:

db.posts.update( 
  { "_id" : { $exists : true }  },
  { $set : { "userCreated.time" : new ISODate("2013-07-11T03:34:54Z") } },
  false,
  true
)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.