我有这样的架构
textview
我想使用embeddedDocsAsJson,原因有两个
如果是数组,可以将重复数据推送到数组,如果我使用json,则不会发生,因为我将 const user = new Schema({
firstName: { type: String, required: true },
lastName: { type: String , required: true},
phone:{type: Number, unique true}
embeddedDocsAsJson: {} // not as an array
},
{ minimize: false }
)
用作unique id
键
检索将更快,因为我不必迭代阵列。我可以从json
问题:
首先我插入json key
。
添加firstName and lastName phone
,同时更新以下文档是我的更新代码
embeddedDocsAsJson
我在更新
时向 let user = await User.findOne({phone: somenumber})
user.embeddedDocsAsJson.someId = someObject // getting error in this line because `user.embeddedDocsAsJson` is `undefined`
user.save()
添加值
答案 0 :(得分:1)
EmbeddedDocs是默认数组,如果你想在你的集合中保存对象,代码就可以了。
let user = await User.findOne({phone: somenumber})
user.embeddedDocsAsJson = {}
user.embeddedDocsAsJson.someId = someObject // getting error in this line because `user.embeddedDocsAsJson` is `undefined`
user.save()