3

For some reason I'm having a hard time getting this to work. I have a Mongo collection called 'cats', and another called 'rows'. The cat field in 'cats' is currentCat. Then I want to be able to update a document in 'rows' by referring to a preexisting document with an _id. No problems there. But for every currentCat, I want to add an element to the 'rows' document with currentCat as the field and something else as the value (currentPer). I came up with this:

var cats = Cats.find({owner: Meteor.userId()}, {fields: {cat: 1, per: 1}});

cats.forEach(function(cats) { //Inserts the rest of the row
  var currentCat = cats['cat'];
  var currentPer = cats['per'];
  var currentAmount = Math.round(amount*currentPer*100)/100;

  Rows.update(
    { _id: row },
    { $push: { currentCat: currentAmount } }
  );
});

(row is the id of the document I want modified)

Of course, it doesn't work as expected and just inserts ...{currentCat: 57} instead of the value of the variable currentCat. Next, I tried creating a query object (as described here):

var query = ({ _id: row }, { $push: { currentCat: currentAmount } })

That also failed, but this time with a wonderfully descriptive error message:

Error: Invalid modifier. Modifier must be an object.

Any help would be appreciated, as I've spent hours and countless Google searches on this (although the answer is probably obvious and I just can't see it - I'm still trying to get used to Meteor after PHP).

EDIT: I want the finished document to look something like this:

 { "_id" : "PshEYKebccw7eYaTo", 
   "createdAt" : ISODate("2014-11-26T00:52:51.840Z"),
   "cat1" : 312,
   "cat2" : 564,
   "owner" : "GiWCb8jXbPfzyc5ZF",
   "text" : "asdf" 
}
5
  • 1
    can you show the json you are expecting to get? Commented Nov 26, 2014 at 2:07
  • @KeithNicholas Added an example to it. Commented Nov 26, 2014 at 2:15
  • What do you want a Row document to look like? Based on your code, I'm expecting something like {_id: 123, currentCat: [5.25, 6.75, 1.55, 2.78]} Commented Nov 26, 2014 at 2:15
  • @Colllin Please see my example. I want to have a field and value pair for each cat. Commented Nov 26, 2014 at 2:25
  • Ok, so basically you missed the point of the query object in that other answer. In Javascript, { currentCat: currentAmount } becomes { 'currentCat': 312 } because the key must be a literal key. You can't use a variable as the key name. In order to do that, you must build up the object as in my answer below. By using the square bracket notation, you can set a dynamic key on the object, and then send that object into the query. catData[currentCat] first interprets the currentCat variable to be 'cat1', and then accesses that key, as if you had done catData['cat1']. Commented Nov 26, 2014 at 2:40

1 Answer 1

2

$push is only for operating on arrays. Based on your example of a Row document, you don't really want an array operation here.

Try just setting fields on the Row document for each new cat value.

  var catData = {};
  catData[currentCat] = currentAmount;
  // As an example, catData now looks like {'cat1': 312}

  Rows.update(
    { _id: row },
    { $set: catData }
  );
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Now works perfectly. I ended up declaring catData before the forEach and put Rows.update() after. Also, as a bonus, I now only access the database once instead of multiple times for each cat.

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.