Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在mongoose中创建带有{strict:false}的文档_Javascript_Node.js_Mongodb_Mongoose - Fatal编程技术网

Javascript 在mongoose中创建带有{strict:false}的文档

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

任务是将一些文档存储到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 };

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);