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

Posted

技术标签:

【中文标题】如何使用另一个集合 mongoose 查找子查询文档【英文标题】:How to lookup subquery documents with another collection moongoose 【发布时间】:2020-04-24 01:06:16 【问题描述】:

我需要加入用户收藏两次,因为同一用户会将产品和购买的用户详细信息发布为子文档。

产品架构:


    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",
    
]

使用已发布的用户 ID 和购买的用户 ID 子数组加入同一个用户表。

请帮助我如何破解这个,在此先感谢。

【问题讨论】:

【参考方案1】:

首先,您需要像这样修复您的 Product 架构以包含 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
    
]

【讨论】:

我指的是用户的另一个集合我如何直接加入用户,你有什么解决方案吗? @jdileep 如果你能提出一个新问题会更好。我会尽量回答。 我发布了另一个问题,你能帮我吗?谢谢

以上是关于如何使用另一个集合 mongoose 查找子查询文档的主要内容,如果未能解决你的问题,请参考以下文章

Mongoose 只查找那些在另一个集合中有条目的人

如何使用 Mongoose 进行查询,获得 N 个结果,但结合它找到的任何满足特定条件的文档?

在猫鼬中,如何根据相关集合中的值查找记录?

Mongoose 聚合查找 - 如何按特定 id 过滤

基于多个子文档的MongoDB/Mongoose查询

如何在 Foreach 中同步运行 Mongoose Find