Meteor.users.update elements within a list within the user object [Meteor]

I have this Sample User that I want to Meteor.users.update upon:

{
  "_id": "BfoZ6uWcaPbNwTLGT",
  "nodeHistory": [
    {
      "_nodeHistoryMomentId": "6dcdc770e820aa985fe3a19b",
      "date": "2015-07-28T19:46:30.002Z",
      "numNodes": 4,
      "endDate": null,
      "priceForClusterTime": null
    }
  ],
  "emails": [
    {
      "address": "[email protected]",
      "verified": false
    }
  ],
  "profile": {
    "name": "alexCl"
  }
}

Specifically, I'm trying to update the endDate and priceForClusterTime fields in a given User's object. I've attempting to update a specific element (by _nodeHistoryMomentId) of the nodeHistory list, but it's not finding the element I'm looking for (and therefore not updating). How do I modify my mongo parameters to perform this update?

Thanks so much!

Server Code:

totalPrice = 200.00
rightNow = new Date()
Meteor.users.update(
   { _id: Uid, "nodeHistory": {"_nodeHistoryMomentId": previousNodeSetup._nodeHistoryMomentId} },
     {
       $set: { 
        endDate: rightNow,
        priceForClusterTime: totalPrice 
      }
   },
   function(error) {console.log('did not find, and/or did not insert')}
);

Upvotes: 0

Views: 38

Answers (2)

David Weldon
David Weldon

Reputation: 64312

Because you are updating an array, you'll need to use the position operator to select the matching element. Give this a try:

var totalPrice = 200.00;
var rightNow = new Date();
var selector = {_id: Uid, 'nodeHistory._nodeHistoryMomentId': indicator._id};
var modifier = {$set: {
  'nodeHistory.$.endDate': rightNow,
  'nodeHistory.$.priceForClusterTime': totalPrice,
}};
Meteor.users.update(selector, modifier);

Upvotes: 0

Michel Floyd
Michel Floyd

Reputation: 20227

Just use the full path to the node to specify it in $set: making sure to put the path in quotes.

{
  $set: { 
    "nodeHistory.endDate": rightNow,
    "nodeHistory.priceForClusterTime": totalPrice 
}

Upvotes: 1

Related Questions