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"
}