如何使用另一个集合猫鼬查找子查询文档

How to lookup subquery documents with another collection moongoose

我需要两次加入用户集合,因为同一用户会将 post 产品和购买的用户详细信息作为子文档。

产品架构:

{
    product_title: String,
    product_desc: String,
    Product_purchased:[
        {
            userid: mongoose.Schema.ObjectId,
            purchased_date: Date
        }]
    posteduserId: mongoose.Schema.ObjectId
}

用户架构:

{
    _id: userId,
    name: String,
    Pic: String
}

示例文档:

[
    {
        "product_title":"title",
        "product_desc":"desc",
        "Product_purchased":[
            {
                "userid":"5d4hvh7duc7c7c8d9scbe",
                "name":"name",
                "Pic":"url",
                "purchased_date":"Date"
            },
            {
                "userid":"5d4hvh7duc7c7c8d9scbe",
                "name":"name",
                "Pic":"url",
                "puchased_date":"Date"
            }
        ],
        "posteduserId": "5d4hvh7duc7c7c8d9scbe",
        "userid": "5d4hvh7duc7c7c8d9scbe",
        "name": "name",
        "pic": "url",
    }
]

使用 posted userid 和购买的 userIds 的子数组加入同一用户 table。

请帮我破解这个,先谢谢了。

首先,您需要像这样修复您的产品架构以包含 ref 字段:

const mongoose = require("mongoose");

const ProductSchema = new mongoose.Schema({
  product_title: String,
  product_desc: String,
  Product_purchased: [
    {
      userid: {
        type: mongoose.Schema.ObjectId,
        ref: "User"
      },
      purchased_date: Date
    }
  ],
  posteduserId: {
    type: mongoose.Schema.ObjectId,
    ref: "User"
  }
});

module.exports = mongoose.model("Product", ProductSchema);

我这样设置用户模型:

const mongoose = require("mongoose");

const UserSchema = new mongoose.Schema({
  name: String,
  Pic: String
});

module.exports = mongoose.model("User", UserSchema);

ref User 值必须与用户模型中的模型名称 User 匹配。

然后你需要像这样填充两次:

router.get("/products", async (req, res) => {
  const result = await Product.find({})
    .populate("Product_purchased.userid")
    .populate("posteduserId");
  res.send(result);
});

假设您有这 3 个用户:

{
    "_id": "5e133deb71e32b6a68478ab4",
    "name": "user1 name",
    "Pic": "user1 pic",
    "__v": 0
},
{
    "_id": "5e133df871e32b6a68478ab5",
    "name": "user2 name",
    "Pic": "user2 pic",
    "__v": 0
},
{
    "_id": "5e133e0271e32b6a68478ab6",
    "name": "user3 name",
    "Pic": "user3 pic",
    "__v": 0
}

而这个产品:

{
    "_id": "5e133e9271e32b6a68478ab8",
    "product_title": "product1 title",
    "product_desc": "product1 description",
    "Product_purchased": [
        {
            "purchased_date": "2020-01-06T14:02:14.029Z",
            "_id": "5e133e9271e32b6a68478aba",
            "userid": "5e133df871e32b6a68478ab5"
        },
        {
            "purchased_date": "2020-01-06T14:02:14.029Z",
            "_id": "5e133e9271e32b6a68478ab9",
            "userid": "5e133e0271e32b6a68478ab6"
        }
    ],
    "posteduserId": "5e133deb71e32b6a68478ab4",
    "__v": 0
}

结果将是:

[
    {
        "_id": "5e133e9271e32b6a68478ab8",
        "product_title": "product1 title",
        "product_desc": "product1 description",
        "Product_purchased": [
            {
                "purchased_date": "2020-01-06T14:02:14.029Z",
                "_id": "5e133e9271e32b6a68478aba",
                "userid": {
                    "_id": "5e133df871e32b6a68478ab5",
                    "name": "user2 name",
                    "Pic": "user2 pic",
                    "__v": 0
                }
            },
            {
                "purchased_date": "2020-01-06T14:02:14.029Z",
                "_id": "5e133e9271e32b6a68478ab9",
                "userid": {
                    "_id": "5e133e0271e32b6a68478ab6",
                    "name": "user3 name",
                    "Pic": "user3 pic",
                    "__v": 0
                }
            }
        ],
        "posteduserId": {
            "_id": "5e133deb71e32b6a68478ab4",
            "name": "user1 name",
            "Pic": "user1 pic",
            "__v": 0
        },
        "__v": 0
    }
]