[英]Creating a document with { strict: false } in mongoose
The task is to store some documents into MongoDB.任务是将一些文档存储到 MongoDB 中。 The documents have the same top-level but from there they could be different.
这些文档具有相同的顶层,但从那里它们可能会有所不同。 The structure of the payload is:
有效载荷的结构是:
{
"types": "a", //the type can be "a", "b" or "c"
"details" : {
... // the details object structure is different for each type
}
}
and this is the model I wrote:这是我写的 model:
const Details = { strict: false };
const MyOrder = new Schema({
types: {
type: String,
enum: ['a', 'b', 'c'],
},
details: Details,
});
module.exports = Order = mongoose.model('myOrder', MyOrder);
I set details with { strict: false }
because I want to get its data no matter what structure it has.我使用
{ strict: false }
设置详细信息,因为无论它具有什么结构,我都想获取它的数据。 Maybe it's wrong something there.也许那里有问题。
When a POST request is done, the document saved into the database it looks like this:完成 POST 请求后,保存到数据库中的文档如下所示:
_id: ObjectId("...")
types: "a"
__v : 0
It saved the types
but nothing about the details.它保存了
types
,但没有任何细节。
Is it a way to save the details too?这也是一种保存细节的方法吗?
I managed to solve the problem by not creating another Details
object like above but adding { strict: false }
inside the Schema.我设法通过不像上面那样创建另一个
Details
object 但在架构中添加{ strict: false }
来解决问题。 Like this:像这样:
const MyOrder = new Schema(
{
types: {
type: String,
enum: ['a', 'b', 'c'],
},
},
{ strict: false }
);
module.exports = Order = mongoose.model('myOrder', MyOrder);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.