我需要将两个表聚合成一个表,这样name就可以从100

100人到id_itemquantity

下面是我要使用的 node 模块

  "dependencies": {
    "connect-mongo": "^5.0.0",
    "dotenv": "^16.0.3",
    "ejs": "^3.1.9",
    "express": "^4.18.2",
    "express-ejs-layouts": "^2.5.1",
    "express-flash": "^0.0.2",
    "express-flash-message": "^3.0.2",
    "express-session": "^1.17.3",
    "method-override": "^3.0.0",
    "mongoose": "^7.2.0",
    "passport": "^0.6.0",
    "passport-google-oauth20": "^2.0.0"
  },

Node.js版本-18.15.0.

这是我的管道

    const customerOrder = await Customer.aggregate([
      {
        $match: {
          _id: customerId,
        },
      },
      {
        $unwind: "$order",
      },
      {
        $lookup: {
          from: "products",
          localField: "order.id_item",
          foreignField: "_id",
          as: "item_details",
        },
      },
      {
        $project: {
          _id: 0,
          id_item: "$order.id_item",
          quantity: "$order.quantity",
          name: { $arrayElemAt: ["$item_details.name", 0] },
        },
      },
    ]);

产品示例

{
  "_id": { "$oid": "646fbe20d4372448890db84c" },
  "name": "Apple Magic Mouse",
  "category": "computer_peripherals",
  "type": "accessories",
  "price": { "$numberInt": "150" },
  "quantity": { "$numberInt": "700" }
}

客户示例

{
  "_id": { "$oid": "646fb71cd437244889fe7404" },
  "firstName": "test",
  "lastName": "testtest",
  "tel": "test",
  "email": "test",
  "address": {
    "country": "test",
    "city": "test",
    "street": "test",
    "zip": "5489"
  },
  "createdAt": { "$date": { "$numberLong": "1684872074610" } },
  "updatedAt": { "$date": { "$numberLong": "1685046572891" } },
  "order": [
    {
      "id_item": "646fbe20d4372448890db84c",
      "quantity": { "$numberInt": "1" },
      "_id": { "$oid": "646fc52c58af0f7b907aaa1c" }
    },
    {
      "id_item": "646fbe20d4372448890db84d",
      "quantity": { "$numberInt": "2" },
      "_id": { "$oid": "646fc52c58af0f7b907aaa1d" }
    },
    {
      "id_item": "646fbe20d4372448890db84e",
      "quantity": { "$numberInt": "110" },
      "_id": { "$oid": "646fc52c58af0f7b907aaa1e" }
    },
    {
      "id_item": "646fbe20d4372448890db84f",
      "quantity": { "$numberInt": "220" },
      "_id": { "$oid": "646fc52c58af0f7b907aaa1f" }
    },
    {
      "id_item": "646fbe20d4372448890db850",
      "quantity": { "$numberInt": "330" },
      "_id": { "$oid": "646fc52c58af0f7b907aaa20" }
    },
    {
      "id_item": "646fc52c58af0f7b907aaa1a",
      "_id": { "$oid": "646fc52c58af0f7b907aaa21" }
    },
    {
      "id_item": "646fc52c58af0f7b907aaa1b",
      "_id": { "$oid": "646fc52c58af0f7b907aaa22" }
    }
  ]
}

这就是我得到的

[
  { id_item: '646fbe20d4372448890db84c', quantity: 1 },
  { id_item: '646fbe20d4372448890db84d', quantity: 2 },
  { id_item: '646fbe20d4372448890db84e', quantity: 0 },
  { id_item: '646fbe20d4372448890db84f', quantity: 0 },
  { id_item: '646fbe20d4372448890db850', quantity: 0 },
  { id_item: '646fc52c58af0f7b907aaa1a' },
  { id_item: '646fc52c58af0f7b907aaa1b' }
]

我也试过了

    const customerOrder = await Customer.aggregate([
      {
        $lookup: {
          from: "products",
          localField: "order.id_item",
          foreignField: "_id",
          as: "productsNew",
        },
      },
    ]);

输出为

  {
    _id: new ObjectId("646fb71cd437244889fe7404"),
    firstName: 'fasdffasfasf',
    lastName: 'Important',
    tel: 'Important',
    email: 'Important',
    address: {
      country: 'Important',
      city: 'Important',
      street: 'Important',
      zip: '5489'
    },
    createdAt: 2023-05-23T20:01:14.610Z,
    updatedAt: 2023-05-25T20:29:32.891Z,
    order: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ],
    productsNew: []
  }

以下是我预期的结果

[
  { name: "Apple Magic Mouse", id_item: '646fbe20d4372448890db84c', quantity: 1 },
  { name: "why it is not working??", id_item: '646fbe20d4372448890db84d', quantity: 2 },
  { name: "name name", id_item: '646fbe20d4372448890db84e', quantity: 0 },
  { name: "lorem name", id_item: '646fbe20d4372448890db84f', quantity: 0 },
  { name: "some name", id_item: '646fbe20d4372448890db850', quantity: 0 },
  { id_item: '646fc52c58af0f7b907aaa1a' },
  { id_item: '646fc52c58af0f7b907aaa1b' }
]

我已经重写了这条管道很多次,阅读了文档,询问了聊天GPT,概念.所以,没有什么帮助

推荐答案

我昨天找到了解决办法. 也许这会对某些人有用. 问题是,我试图将字符串与对象ID进行比较,我只需在产品中创建一个id字段,一切都会正常工作

老产品

{
  "_id": { "$oid": "646fbe20d4372448890db84c" },
  "name": "Apple Magic Mouse",
  "category": "computer_peripherals",
  "type": "accessories",
  "price": { "$numberInt": "150" },
  "quantity": { "$numberInt": "700" }
}

新产品

{
  "_id": { "$oid": "646fbe20d4372448890db84c" },
  "id": "646fbe20d4372448890db84c",
  "name": "Apple Magic Mouse",
  "category": "computer_peripherals",
  "type": "accessories",
  "price": { "$numberInt": "150" },
  "quantity": { "$numberInt": "700" }
}

现在我得到了这个输出

.

  {
    id_item: '646fbe20d4372448890db84c',
    quantity: 1,
    name: 'Apple Magic Mouse'
  },
  { id_item: '646fbe20d4372448890db84d', quantity: 2 },
  { id_item: '646fbe20d4372448890db84e', quantity: 0 },
  { id_item: '646fbe20d4372448890db84f', quantity: 0 },
  { id_item: '646fbe20d4372448890db850', quantity: 0 },
  { id_item: '646fc52c58af0f7b907aaa1a' },
  { id_item: '646fc52c58af0f7b907aaa1b' }
]

所以我需要修改所有的产品.

Node.js相关问答推荐

编辑Mongoose中的对象嵌套数组

聚合发布/订阅消息

Mongoose查询-如何根据当前查找ID获取其他集合并将其插入到当前查找中?

在编译时强制不缩小类型范围

编写动态创建的 YAML - 为生态系统创建 Docker compose 文件(使用 js-yaml)

Postgressql的BIGSERIAL自增序列,即使由于唯一约束错误没有创建行,也会自动增加

具有项目外部子路径导入的 Firebase 函数

只要我在后端正确验证所有内容,就可以将这些数据存储在本地存储中吗?

如何使用 NodeJS 加密模块将 ECDH 密钥转换为 PEM 格式

无法使用 node 预签名 url 从 React 将图像文件上传到 s3

Typescript typeRoots 未检测到类型定义

nvm / node / npm: node 12 的 npm 比 node 14 的更新?

Mongodb 从文档中获取聚合结果中的特定属性

将已保存的卡片从条带显示到前端

使用 firebase 函数 api 运行套接字(相同的端口创建问题)

如何刷新 youtube-data-api v3 的访问令牌

如何undo撤消 Object.defineProperty 调用?

Dart 语言比 JavaScript (Node.js) 有什么好处

将 Heroku App 连接到 Atlas MongoDB 云服务

在 Node.js 上使用 Connect 无法获取 /