首页 > 解决方案 > $push 创建一个只有 _id 的新对象,没有属性——feathsjs+mongoose

问题描述

我正在尝试使用 $push 将对象(具有一个属性,文本)插入到子文档的数组中:

await this.app.service("someService").patch(
      null,
      {
        $push: { "subDocuments.$.textArray": { text: "stuff" }}
      },
      {
        query: {
          _id: parentId,
          "subDocuments._id": subDocumentId
        }
      }
    );
  }

该模型如下所示:

const ParentSchema = new Schema({
    subDocuments: [SubDocumentSchema]
});

const SubDocumentSchema = new Schema({ 
     textArray: [
          {
              text: { type: String }
          }
     ]
});

该对象已创建,但它是一个以 _id 作为其唯一属性的空对象。我错过了什么?

标签: javascriptmongodbmongoosemongoose-schemafeathersjs

解决方案


请声明架构,如:

const ParentSchema = new Schema({
    subDocuments: [SubDocumentSchema]
});

const SubDocumentSchema = new Schema({ 
     textArray: [
          {
              text: { type: String }
          }
     ]
}, { _id: false });

推荐阅读