Updating an item in an array with a unique identifier

I have a document structure like this:

Content { _id: "mongoId" Title: "Test", Age: "5", Peers: [{uniquePeer: "1", Name: "Testy Test", lastModified: "Never"}, {uniquePeer: "2", Name: "Test Tester", lastModified: "Never"}] } 

So Peers is an array that has a unique identifier. How can I update lastModified of one of the sets in an array? According to mongodb, I can only update the document using the unique identifier of the document, but at the top level. How can I say update this lastModified field in this Peers set with uniquePeer from 1 in this document?

Edit:

 Content.update({"_id" : "mongoId", "Peers.uniquePeer" : "1"},{$set : {"Peers.$.lastModified" : "Now"}}) 

I still get the message "Not Allowed. Invalid code can only update documents by ID."

+2
source share
1 answer

See docs for updating the array. Your code should look something like this:

server

 Meteor.methods({ 'content.update.lastModified': function(contentId, peerId) { check(contentId, String); check(peerId, String); var selector = {_id : id, 'Peers.uniquePeer': peerId}; var modifier = {$set: {'Peers.$.lastModified': 'Now'}}; Content.update(selector, modifier); } }) 

client

 Meteor.call('content.update.lastModified', contentId, peerId); 

Please note that this type of operation must be performed in a server-specific method, because, as you found out, you can only update documents by id on the client.

+4
source

All Articles