How to update an meteor collection array

4,731 views
Skip to first unread message

Seba

unread,
Aug 24, 2013, 10:24:47 AM8/24/13
to meteo...@googlegroups.com
Hello :)

I want to update an array (meteor, mongodb). I can add some values to the array, but I can't update an specific position in the array like the fifth.

That's my array:
points: [5,4,8,3,4,0]

Here is my not working code:
Books.update({_id:"selected_player"}, {$addToSet: {points[5]: 17}});

I want to add the number 17 to field 5

James Wilson

unread,
Aug 24, 2013, 11:54:22 AM8/24/13
to meteo...@googlegroups.com
try 

Books.update('
RZQ8M8ooXyF9Bxdcv', {$addToSet: {points[5]: 17}});

or similar as the first argument should be a collectionObject._id string property.


James Wilson


--
You received this message because you are subscribed to the Google Groups "meteor-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to meteor-talk...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Arunoda Susiripala

unread,
Aug 24, 2013, 11:58:27 AM8/24/13
to meteo...@googlegroups.com

Seba

unread,
Aug 24, 2013, 12:01:07 PM8/24/13
to meteo...@googlegroups.com, Ja...@jameswilson.name
Hi James,

the problem is not the _id. The problem is [5], because of this error in JavaScript console:
Uncaught SyntaxError: Unexpected token ]

Seba

unread,
Aug 24, 2013, 2:07:26 PM8/24/13
to meteo...@googlegroups.com
Hi :)
It doesn't matter which operator (addToSet or push) I'm using. It's always the same.

with:
Books.update({_id:"selected_player"}, {$addToSet: {points[5]: 17}});

or:
Books.update({_id:"selected_player"}, {$push: {points[5]: 17}});

It doesn't work and if I try this:
Books.update({_id:"selected_player"}, {$addToSet: {points: 17}});

I get this:
points: [5,4,8,3,4,0, 17]

but I want this:
points: [5,4,8,3,4,17]


Sorry for my bad english, I  hope I could explain my problem to you.

Barrett Snyder

unread,
Aug 24, 2013, 2:58:26 PM8/24/13
to meteo...@googlegroups.com
I'm new to meteor and could be wrong on this, but I think you may need to manually modify the array then $set the entire array on the player object.

function updatePlayerPoints(player_id, points_idx, point_val) {
var p = players.findOne({"_id":player_id});
if (p&&p.points) {
p.points[points_idx] = point_val;
players.update({"_id":player_id},{$set:{"points":p.points}});
}
};

Then call it with:
updatePlayerPoints(Session.get('selected_player'), 5, 17);
Message has been deleted

Seb

unread,
Aug 24, 2013, 6:08:13 PM8/24/13
to meteo...@googlegroups.com
I'm shocked. I can't believe that it is so complicated. 

Is there no other way to insert/update a value to a specific field in an array? 

Seb

unread,
Aug 24, 2013, 6:15:52 PM8/24/13
to meteo...@googlegroups.com


Here is the solution:
Books.update({_id:"selected_player"}, {$set: {"points.5": 17}});

Barrett Snyder

unread,
Aug 24, 2013, 11:23:55 PM8/24/13
to meteo...@googlegroups.com
Cool, that's much easier! :)
Reply all
Reply to author
Forward
0 new messages