我应用了$Match条件,但响应既没有变得更合适,也没有收到错误. 我想要经过过滤的记录,其中我传递了page_id,并期望所有版本都详细说明与传递的page_id映射的版本.

以下是我的代码 版本模型

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
  }
]

预期react

[
  {
    "_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值为"64b77c1f2712e47f8211aae0"的所有版本名

我是Node的新手 请帮帮忙.

以下是收集的详细信息

集合:页面版本

集合:页面版本

集合:版本

集合:版本

推荐答案

要按page_id筛选结果,应在aggregate函数的开头添加$match阶段.

try

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"
        }
    }
]);

Node.js相关问答推荐

Windows上使用ES6+的OpenAPI规范的Express服务器不接受嵌套路由'

如何使用jq将依赖项添加到package.json中

如何使用Node.js、Express和Mongoose创建多个API

编辑Mongoose中的对象嵌套数组

从目录中获取所有文件,而不是NodeJS中的单个文件

如何发送比特币BTC使用发送加密使用WIF密钥在 node ,js

我的 React + Express 应用程序不断向我的数组添加一个空对象

遇到 - TypeError:try 使用 Express(Node.js) 从 JSON 文件访问数据时无法读取未定义的属性(读取帖子)

如何避免在 mongodb 聚合中查找重复结果?

如何使用 node 在 koa.js 中发送响应

为什么我的react 表单不能正常工作?

FirebaseCloudMessaging : PlatformException (PlatformException(null-error, Host platform returned null value for non-null return value., null, null))

仅显示用户在 Reactjs 中使用服务器端发布的帖子是 Node Js、Mongodb

为什么需要在 NodeJS 应用程序中创建服务器?

如何更改NodeJS中的堆栈大小限制?

在 ExpressJS 中将变量传递给 JavaScript

如何undo撤消 Object.defineProperty 调用?

在 Node.js 中的两个不同进程之间进行通信

在 Node.js 中使用公钥加密数据

如何阻止 babel 将this转换为undefined(并插入use strict)