如何从猫鼬的引用模型中获取字段?

时间:2018-10-28 06:45:22

标签: mongodb mongoose mongodb4.0

嘿,在查询方面需要一点帮助,以下是该模型,我正在将其引用到另一个称为项目的模型

const User= new mongoose.Schema({
  featured_projects: {
    type: [
      {
        type: Schema.Types.ObjectId,
        ref: 'Project'
      }
    ],
  }
}, {
    timestamps: true,
  });

//在下面输出

{
    "featured_projects" : ["5bd54c7d65194f200d1c6e2d","5bd41b953fc9f040b08995df"]
}

项目模型存储为

{
                "en": {
                    "isActive": true,
                    "amneties": [],
                    "carasouel_images": [],
                    "location": "location (en)",
                    "name": "name (en)"
                },
                "th": {
                    "isActive": true,
                    "amneties": [],
                    "carasouel_images": [],
                    "location": "location (th)",
                    "name": "name (th)"
                },
                "_id": "5bd54c7d65194f200d1c6e2d",
                "createdAt": "2018-10-28T05:43:25.982Z",
                "updatedAt": "2018-10-28T05:43:25.982Z",
                "__v": 0
            },

我正试图从查询中获取“ en”

this.find(options)
      .populate({
        path: 'featured_projects',
        populate:'en'
      })
      .sort({ createdAt: - 1 })
      .skip(perPage * (page - 1))
      .limit(perPage)
      .exec();

但是不起作用,非常感谢您的帮助,也非常感谢您的建议。

但是,如果我直接尝试从项目中获取“ en”字段,那么我会这样做并且正在工作

this.find(options)
      .select({ [lang]: 1 })
      .sort({ createdAt: -1 })
      .skip(perPage * (page - 1))
      .limit(perPage)
      .exec();

提前谢谢! :)

0 个答案:

没有答案
相关问题