【问题标题】:Mongoose $lookup populates ObjectId as an arrayMongoose $lookup 将 ObjectId 填充为数组
【发布时间】:2021-08-25 21:34:26
【问题描述】:

我有 2 个架构,翻译和设施。 这是翻译架构:

const Translation = mongoose.model('Translation', new Schema({
  az: {
    type: String,
    trim: true,
    required: true
  },
  en: {
    type: String,
    trim: true,
    default: ''
  },
  ru: {
    type: String,
    trim: true,
    default: ''
  }
}, { timestamps: true }), "translations");

下面是设施模式:

const TourismResourceFacility = mongoose.model('TourismResourceFacility', new Schema({
  name: {
    type: Schema.Types.ObjectId,
    ref: "Translation",
    required: true
  }
}, { timestamps: true }), "tourism_resource_facilities");

现在我需要获取带有分页的匹配设施列表并填充引用翻译模式的名称字段。这是查询:

const list = await TourismResourceFacility.aggregate([
                { $match: match }, // this is generated dynamically, so it works as expected
                {
                    $lookup: {
                        from: "translations",
                        localField: "name",
                        foreignField: "_id",
                        as: "name"
                    }
                }
            ])
                .sort(sort)
                .skip(_page * _limit)
                .limit(_limit)
                .project('createdAt name.az name.ru name.en');

结果如下:

    [
        {
            "_id": "60bf758bf6e70e775a9ab64d",
            "name": [
                {
                    "en": "",
                    "ru": "",
                    "az": "test1"
                }
            ],
            "createdAt": "2021-06-08T13:50:03.136Z"
        },
        {
            "_id": "60bf740b06ca102d004145ca",
            "name": [
                {
                    "en": "",
                    "ru": "",
                    "az": "test123"
                }
            ],
            "createdAt": "2021-06-08T13:43:39.570Z"
        },
        {
            "_id": "60bf704c7f55d97090e744f9",
            "name": [
                {
                    "en": "",
                    "ru": "",
                    "az": "test57"
                }
            ],
            "createdAt": "2021-06-08T13:27:40.969Z"
        },
        {
            "_id": "60bf70127f55d97090e744f7",
            "name": [
                {
                    "en": "",
                    "ru": "",
                    "az": "qwerty"
                }
            ],
            "createdAt": "2021-06-08T13:26:42.149Z"
        },
        {
            "_id": "60bf128e61f65c7b3f6c04a0",
            "name": [
                {
                    "en": "",
                    "ru": "",
                    "az": "f7"
                }
            ],
            "createdAt": "2021-06-08T06:47:42.531Z"
        }
    ]

问题在于,名称字段被填充为列表,而在 Schema 中,它是 ObjectId 的引用,而不是数组。我需要将名称字段作为单个对象获取,例如 {az: "", en: "", ru: ""},而不是在列表中。

谁能帮我创建正确的猫鼬查询?

【问题讨论】:

  • 为什么不用猫鼬的populate()

标签: mongoose aggregate


【解决方案1】:

我找到了解决方案。以防万一有人需要。 {$unwind: "$name"} 是解决方案,而 $name 是我的猫鼬模型中的属性名称。

const list = await this.model.aggregate([
                { $match: match },
                {
                    $lookup: {
                        from: "translations",
                        localField: "name",
                        foreignField: "_id",
                        as: "name"
                    }
                },
                { $unwind: "$name" }
            ])
                .sort(sort)
                .skip(_page * _limit)
                .limit(_limit)
                .project('createdAt name.az name.ru name.en');

【讨论】:

    猜你喜欢
    • 2017-04-20
    • 2021-03-03
    • 2020-10-14
    • 2021-01-02
    • 2016-05-09
    • 2019-02-13
    • 1970-01-01
    • 2019-01-31
    • 2014-12-05
    相关资源
    最近更新 更多