1

I'm trying to do something tricky but I'm not sure if that's the way to go. Here it is :

I have an user with object dispos. Dispos contains arrays of months, so can be

user.dispos: {"02-2015" : [], "05-2015" : [] }

Now, I want to do a dynamic update of the specific month, for example I want to only update the user.dispos["02-2015"] field and not replace the whole dispos object, letting the ["05-2015"] remaining. For now I tried :

User.update({ _id:  req.body.id }, 
{ $set: { dispos["02-2015"]: disposOf-02-2015} }, function(error){
   if (error) return res.send(500, err);
   return res.send(204) });

It doesn't work, but you get what I mean... I want to update dispos at a certain position, not replacing the whole property. Thanks for your help.

edit

It's nearly there by using the dot notation and wrapping the property name in quotes. I'm using a variable to set the month-year of dispos. With $set: { "dispos.monthYear": dispos } I get dispos : { "monthYear" : { "06-2015 : [] ...} }. Well, I just dont need this "monthYear" level which should be the dynamic mont-year name. In this case monthYear = "06-2015" so I would like to get dispos : { "06-2015": []...} which doesn't erase my other dispos {"07-2015"}

2
  • What's disposOf-02-2015? Or was that supposed to be text "disposOf-02-2015"? Commented Jun 24, 2015 at 23:30
  • it's an example of object containing dispos for 02-2015. Could be named just dispos and containing an object {"02-2015" : [] } Commented Jun 25, 2015 at 0:05

1 Answer 1

3

You need to create the $set object dynamically beforehand.

var $set = { $set: {} };
$set.$set['dispos.' + monthYear] = dispose;
// $set => { $set: { "dispos.02-2015": "disposOf-02-2015"} }

Then use this $set in your query

User.update({_id:…}, $set ,function(err){…});

BTW, what you originally came up with might actually work in future versions of JS.

Sign up to request clarification or add additional context in comments.

5 Comments

It's nearly there... I'm using a variable to set the month-year of dispos. With "dispos.monthYear" : dispos I get dispos : { "monthYear" : { "06-2015 : [] ...} }. Well, I just dont need this "monthYear" level which should be the dynamic mont-year name. In this case monthYear = "06-2015" so I would like to get dispos : { "06-2015": []...}
great, that seems to work fine! thank you. The last question would be concerning the issue when I have multiple months to update. When I do my update once, it returns a response and that's it, but I would like to iterate with a for loop and do something like : 1st) loop -> set.$set.dispo[month1] = month 1-> update, 2nd loop->set.$set.dispo[month2] = month2 -> update.
@ginold and are you facing a problem when you're trying to do this? Could you expand on that in more details, with any errors that you're getting. Perhaps best if it was asked as another question as it kinda seems entirely different scenario, but feel free to update your original post and ping me to check it out.
I'll be creating a new question when I come back to this problem later. For now, you may maybe answer me if it's possible to create anything within a model with an artificial ID and delete that thing without having any model? I mean for ex: myExistingModel : { id : 1232, notExistingModel: { id: objectId, foo:bar } } . Now I would like to look in the database for anything that has that artificially created objectID, because there is no such model.
Or at least look for it in the known collection. I tried db.getCollection('attributions').findOne({daSuperId: ObjectId("558d4c911260d4dc4d50adb4") } ) (that i created) but returns null.

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.