【问题标题】:Mongoose: Filter collection based on values in another collectionMongoose:根据另一个集合中的值过滤集合
【发布时间】:2018-12-11 10:10:02
【问题描述】:

考虑包含另一个集合的 ID 数组的文档,我如何使用 mongoose 根据相关集合找到应用过滤器的文档? 我找不到我的错误

  Installation.aggregate(                        
                    [
                          // Stage 1
                          {
                                $lookup: {
                                      from: "users",
                                      localField: "_id",
                                      foreignField: "_id",
                                      as: "Users"
                                }
                          },

                          // Stage 2
                          {
                                $unwind: {
                                      path: "$Users"
                                }
                          },
                          // Stage 3
                          {
                                $match: 
                                      {"Users.Email1" : "test@test.com"}

                          }, 
                          {
                                $sort: { _id: -1 }
                          },
                          {
                                $limit: 10
                          }                            
                    ]
              ,function (err, installations) {

                   console.log(installations); //<<<<< Empty
                    /*
                    Installation.populate( 'Parent', '_id Email1','Company'), function(err,results) {
                          Installation.count(filter).exec(function (err, count) {
                                res.send({ success: true, results: installations, total: count });
                          });
                    };
                    */                                                             
              });

提前致谢 安德烈亚

【问题讨论】:

  • 如果您在问题中添加一些示例数据会更容易回答
  • 在第 3 阶段之前你能得到结果吗?
  • 输入filter、sorting、_count、req.params.page、req.param.count的值?
  • 完成了我的英雄 :-)
  • 能不能在console.log(installations)前加`**console.log(err)**;???

标签: mongoose mongodb-query mongoose-schema mongoose-populate


【解决方案1】:

您可以使用聚合查询来做到这一点:

db.collectionA.aggregate(

    // Pipeline
    [
        // Stage 1
        {
            $lookup: {
                from: "collectionB",
                localField: "_id",
                foreignField: "_id",
                as: "collectionBData"
            }
        },

        // Stage 2
        {
            $unwind: {
                path : "$collectionBData"
            }
        },

        // Stage 3
        {
            $match: {
                "collectionBData.email": "your@email.address"
            }
        },

    ]

);

希望这能解决您的问题。

此外,在 Mongo 中,我们使用命名约定为 collections 而不是 table_id 而不是 id

【讨论】:

  • 非常感谢,但我不知道应该放在哪里?进入架构?
  • 不,这是一个查询。
  • 我应该使用聚合来代替 find 吗?
  • 'COLLECTIONNAME.aggregate()' 就是这样
  • 您可以在聚合查询中添加排序。聚合是替换查找
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-01
  • 2018-02-14
  • 1970-01-01
  • 2014-11-27
  • 1970-01-01
  • 2010-10-17
  • 2023-03-23
相关资源
最近更新 更多