Javascript 在mongoose中创建带有{strict:false}的文档
任务是将一些文档存储到MongoDB中。这些文档具有相同的顶层,但它们可能会有所不同。 有效载荷的结构是:Javascript 在mongoose中创建带有{strict:false}的文档,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,任务是将一些文档存储到MongoDB中。这些文档具有相同的顶层,但它们可能会有所不同。 有效载荷的结构是: { "types": "a", //the type can be "a", "b" or "c" "details" : { ... // the details object structure is different for each type } } 这就是我写的模型: const Details = { strict: false }; con
{
"types": "a", //the type can be "a", "b" or "c"
"details" : {
... // the details object structure is different for each type
}
}
这就是我写的模型:
const Details = { strict: false };
const MyOrder = new Schema({
types: {
type: String,
enum: ['a', 'b', 'c'],
},
details: Details,
});
module.exports = Order = mongoose.model('myOrder', MyOrder);
我使用{strict:false}
设置详细信息,因为我想获取它的数据,不管它有什么结构。也许是哪里出了问题
完成POST请求后,文档将保存到数据库中,如下所示:
_id: ObjectId("...")
types: "a"
__v : 0
它保存了类型
,但没有保存详细信息
这也是保存细节的一种方法吗?我没有像上面那样创建另一个
details
对象,而是在模式中添加{strict:false}
来解决这个问题。像这样:
const MyOrder = new Schema(
{
types: {
type: String,
enum: ['a', 'b', 'c'],
},
},
{ strict: false }
);
module.exports = Order = mongoose.model('myOrder', MyOrder);
我没有像上面那样创建另一个
Details
对象,而是在模式中添加{strict:false}
来解决这个问题。像这样:
const MyOrder = new Schema(
{
types: {
type: String,
enum: ['a', 'b', 'c'],
},
},
{ strict: false }
);
module.exports = Order = mongoose.model('myOrder', MyOrder);