【问题标题】:$push creates a new object with _id only, without properties - feathsjs+mongoose$push 创建一个只有 _id 的新对象,没有属性——feathsjs+mongoose
【发布时间】:2019-06-20 17:19:13
【问题描述】:

我正在尝试使用 $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 作为其唯一属性的空对象。 我错过了什么?

【问题讨论】:

    标签: javascript mongodb mongoose mongoose-schema feathersjs


    【解决方案1】:

    请像这样声明架构:

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

    【讨论】:

    • 不会禁止为子文档创建 id 吗?那不是我要找的。我正在尝试使用 text 属性创建数组对象(textArray
    • @C493d 您的查询和推送语句看起来不错。我在本地 MongoDB 查询中创建了相同的文档,运行良好。可以参考stackoverflow.com/questions/30840945/push-item-in-sub-document
    • 谢谢,这就是我问的原因,因为我不明白为什么它不起作用。可能是feathersjs的问题
    猜你喜欢
    • 2022-01-24
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    相关资源
    最近更新 更多