使用 Mongoose 更新嵌套对象
Update Nested Objects with Mongoose
我正在尝试使用 Mongoose 更新嵌套对象。但是当我收到请求时,它看起来像这样:
{
'example[apple]': 'false',
'example[pear]': 'false',
'example[banana]': 'false',
'example[orange]': 'false',
}
我的模型是这样的:
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}
}
我发送的对象如下所示:
var formData = {
example: {
apple: false,
banana: false,
pear: false,
orange: false
}
}
我做错了什么?
首先是请求正文
{
'example[apple]': 'false',
'example[pear]': 'false',
'example[banana]': 'false',
'example[orange]': 'false',
}
是一个 JSON 对象,您可以访问:example.apple 等等。
exampleObject 必须首先是 JSON 对象。
从您的模型看来,示例是模型中的属性,因此要更新您必须选择:
1- 是通过不提供文档的 id 来更新所有文档:
yourmodel.findAndUpdate({no thing here},exampleObject,callBack)
2-是按条件更新;首选:
yourmodel.findAndUpdate(condition,exampleObject,callBack)
条件可以是 = {_id:an id}
我正在尝试使用 Mongoose 更新嵌套对象。但是当我收到请求时,它看起来像这样:
{
'example[apple]': 'false',
'example[pear]': 'false',
'example[banana]': 'false',
'example[orange]': 'false',
}
我的模型是这样的:
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}
}
我发送的对象如下所示:
var formData = {
example: {
apple: false,
banana: false,
pear: false,
orange: false
}
}
我做错了什么?
首先是请求正文
{
'example[apple]': 'false',
'example[pear]': 'false',
'example[banana]': 'false',
'example[orange]': 'false',
}
是一个 JSON 对象,您可以访问:example.apple 等等。
exampleObject 必须首先是 JSON 对象。
从您的模型看来,示例是模型中的属性,因此要更新您必须选择:
1- 是通过不提供文档的 id 来更新所有文档:
yourmodel.findAndUpdate({no thing here},exampleObject,callBack)
2-是按条件更新;首选:
yourmodel.findAndUpdate(condition,exampleObject,callBack)
条件可以是 = {_id:an id}