[英]Update Nested Objects with Mongoose
I am trying to update nested objects with Mongoose. 我试图用猫鼬更新嵌套对象。 But when I receive the request, it looks like this: 但是,当我收到请求时,它看起来像这样:
{
'example[apple]': 'false',
'example[pear]': 'false',
'example[banana]': 'false',
'example[orange]': 'false',
}
My model looks like this: 我的模型如下所示:
email: {
type: String,
index:true,
unique: true,
},
example: {
apple: {type:Boolean, default: true},
banana: {type:Boolean, default: true},
pear: {type:Boolean, default: true},
orange: {type:Boolean, default: true}
}
And the object I am sending looks like this: 我发送的对象看起来像这样:
var formData = {
example: {
apple: false,
banana: false,
pear: false,
orange: false
}
}
What am I doing wrong? 我究竟做错了什么?
First of all, the request body { 'example[apple]': 'false', 'example[pear]': 'false', 'example[banana]': 'false', 'example[orange]': 'false', }
is a JSON object you can access: example.apple and so on. 首先,请求正文{ 'example[apple]': 'false', 'example[pear]': 'false', 'example[banana]': 'false', 'example[orange]': 'false', }
是您可以访问的JSON对象:example.apple等。
exampleObject must be JSON object first. exampleObject必须首先是JSON对象。 From your model it seems example is attribute in the model so to update you have to options: 从您的模型看来,示例是模型中的属性,因此要进行更新,您必须选择以下选项:
1- Is to update all documents by not providing the id of the document: 1-是通过不提供文档ID来更新所有文档:
yourmodel.findAndUpdate({no thing here},exampleObject,callBack)
2-Is to update by condition;preferred: yourmodel.findAndUpdate(condition,exampleObject,callBack)
2-根据条件进行更新;首选: yourmodel.findAndUpdate(condition,exampleObject,callBack)
condition can be = {_id:an id}
条件可以是= {_id:an id}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.