Meteor update a Collection object

ぐ巨炮叔叔 提交于 2019-12-25 03:44:15

问题


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?


回答1:


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



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/29634331/meteor-update-a-collection-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!