How to remove an array element in a meteor using mongo?

I have this application I'm working on ...

http://stevedavis.meteor.com/

You can see the contents of group collections by running "console.find ()" in the console.

I have this in my JS ...

Template.listGroups.events({ 'click .deleteMember': function(){ var groupID = this.groupID, firstName = this.firstName, lastName = this.lastName; } }); 

So, I want to be able to remove a member from the group if I click on X next to their name. I tried...

 Groups.update( {"_id": groupID }, {$unset: { "members" : {"firstName": firstName, "lastName": lastName} } } ); 

but he deleted ALL items. I just want it to delete the member element that matches the first and last click name of the element. Thanks.

+4
source share
2 answers

Ah, I just had to change $ unset to $ pull. I added via "$ push", so I thought: "Is there a $ pull method?" And it was! :)

 Groups.update( {"_id": groupID }, {"$pull": { "members" : {"firstName": firstName, "lastName": lastName} } } ); 
+9
source

I found this amazing page in the MongoDB documentation that shows the query / project you found here and more!

MongoDB documentation.

Thank you for asking about this - your answer got me on the right track.

0
source

All Articles