mongodb 如何在mongoose中获得单id数据的记录

ejk8hzay  于 2023-08-04  发布在  Go
关注(0)|答案(1)|浏览(102)

我应用了$match条件,但响应既没有变得更合适,也没有得到错误。我希望在传递page_id的地方有fileted记录,并期望所有版本都详细说明为与传递的page_idMap的版本。
下面是我的代码版本模型

const mongoose = require("mongoose");
const versionSchema = new mongoose.Schema({
    version_name: { type: String, required: true },
    version_slug: { type: String, required: true },
    is_active: { type: Boolean, required: true },
    is_deleted: { type: Boolean, required: true }
}, {
    timestamps: true,
    versionKey: false
});
module.exports = mongoose.model("version", versionSchema);

字符串
页面版本模型

const mongoose = require("mongoose");
const { Schema } = mongoose;
const pageSchema = new mongoose.Schema({
    page_id: { type: Schema.Types.ObjectId, ref: 'pages', require: true }, 
    version_id: { type: Schema.Types.ObjectId, ref: 'versions', require: true },
}, {
    timestamps: true,
    versionKey: false
});
module.exports = mongoose.model("pageVersion", pageSchema);


控制器

pageversions.aggregate([
        {
            $lookup: {
                from: "versions",
                localField:"version_id",
                foreignField:"_id",
                as: "version"
            }
        },
        { $unwind: "$version" },
        {
            $project: {
                page_id:1,
                version_name: "$version.version_name",
                virsion_slug: "$version.version_slug",
                is_active: "$version.is_active"
            }
        }
    ]);
};


获取响应

[
  {
    "_id": "64b77c1f2712e47f8211aae2",
    "page_id": "64b77c1f2712e47f8211aae0",
    "version_name": "V2",
    "virsion_slug": "v2",
    "is_active": true
  },
  {
    "_id": "64b78219a56c70b185045f7b",
    "page_id": "64b77c1f2712e47f8211aae0",
    "version_name": "V1",
    "virsion_slug": "v1",
    "is_active": true
  },
  {
    "_id": "64b786251a4b4aa03e869adb",
    "page_id": "64b786251a4b4aa03e869ad9",
    "version_name": "V1",
    "virsion_slug": "v1",
    "is_active": true
  }
]


预期响应

[
  {
    "_id": "64b77c1f2712e47f8211aae2",
    "page_id": "64b77c1f2712e47f8211aae0",
    "version_name": "V2",
    "virsion_slug": "v2",
    "is_active": true
  },
  {
    "_id": "64b78219a56c70b185045f7b",
    "page_id": "64b77c1f2712e47f8211aae0",
    "version_name": "V1",
    "virsion_slug": "v1",
    "is_active": true
  }
]


我想要page_id值为“64 b77 c1 f2712 e47 f8211 aae 0”的所有版本名
我是新来的Node好心帮忙。
这里是收集详细信息
集合:页面版本
x1c 0d1x的数据
集合:版本


ttcibm8c

ttcibm8c1#

要按page_id过滤结果,您应该在aggregate函数的开头添加一个$match阶段。
试试看

pageversions.aggregate([
    {
        $match: {
            page_id: mongoose.Types.ObjectId("64b77c1f2712e47f8211aae0")
        }
    },
    {
        $lookup: {
            from: "versions",
            localField:"version_id",
            foreignField:"_id",
            as: "version"
        }
    },
    { $unwind: "$version" },
    {
        $project: {
            page_id:1,
            version_name: "$version.version_name",
            virsion_slug: "$version.version_slug",
            is_active: "$version.is_active"
        }
    }
]);

字符串

相关问题