0

我在这里阅读了一些推荐所有这 3 个选项的答案,但在实践中它们对我不起作用。
即使保存后,文档变量也没有 customField,并且文档在没有 customField 的情况下保存到数据库中。

    export const MySchema = new mongoose.Schema(
      {
        createdAt: { type: Date, default: Date.now }
      },
      { strict: false }
    );

    const MyModel = mongoose.model('MyModel', MySchema);

    // This didn't work #1
    const [doc1] = await MyModel.create([{ customField: "some" }], { strict: false });

    // This didn't work #2
    let doc2 = new MyModel({ customField: "some" });
    doc2.save({ strict: false });

    // This didn't work #3
    let doc3 = new MyModel();
    doc3.set("customField", "some");
    doc3.save({ strict: false });

注意:这是示例代码,可能有一些错别字

4

0 回答 0