gpt4 book ai didi

javascript - 使用 Mongoose 更新嵌套对象

转载 作者:行者123 更新时间:2023-12-03 05:44:52 25 4
gpt4 key购买 nike

我正在尝试使用 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
}
}

我做错了什么?

最佳答案

首先是请求体 {
'示例[苹果]': '假',
'示例[梨]': '假',
'示例[香蕉]': '假',
'示例[橙色]': '假',
}
是您可以访问的 JSON 对象:example.apple 等。
exampleObject 首先必须是 JSON 对象。从您的模型来看,示例似乎是模型中的属性,因此要更新您必须选择:
1- 是通过不提供文档的 id 来更新所有文档:
yourmodel.findAndUpdate({这里没有任何东西},exampleObject,callBack)
2-是按条件更新;首选: yourmodel.findAndUpdate(condition,exampleObject,callBack)
条件可以是= {_id:an id}

关于javascript - 使用 Mongoose 更新嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40371513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com