user841760
user841760

Reputation: 359

Meteor update a Collection object

in Meteor, I'm having a collection with a Schema, and a number of items are added dynamically.

In this case, I'm dealing with milestones object, and once the user check one off I want to update complete in this Collections item to true (default is false)

Here is my schema

milestones: {
type: Array,
optional: true
},
'milestones.$': {
type: Object
},
'milestones.$.name': {
type: String
},
'milestones.$.hours': {
type: Number
},
'milestones.$.complete': {
type: Boolean
}

How do I write a $set statement for this?

Upvotes: 0

Views: 844

Answers (3)

user841760
user841760

Reputation: 359

So thanks to Aldeed I found a solution - which needs to be called on server side, otherwise it won't let the update happen. Do:

Projects.update({_id:currentPostId, 'milestones.name':name}, {$set:{'milestones.$.complete':true}});

The function is called on the client with Meteor.call with all needed params.

Upvotes: 0

Ethaan
Ethaan

Reputation: 11376

You have an array of objects so, $elemMatch do the trick here.

Projects.update({_id:this._id},{milestones:{$elemMatch:{'milestones.$‌​.name':this.name}},{$set:{'milestone.$.complete':value}}})

Upvotes: 2

255kb - Mockoon
255kb - Mockoon

Reputation: 6974

According to your schema you have an object containing an array of objects. So you should write you $set like this:

{$set: {'milestone.$.complete':value}}

This will update the first array element corresponding to the query.

You can find here the official documentation if you want to know more about arrays updates in Mongo.

Upvotes: -1

Related Questions