node.js - 如何根据填充中的多个条件过滤 Mongoose 中的结果?

标签 node.js mongodb mongoose

我有一个要显示的用户列表。我有 2 个过滤器

1) 区域

2) 类别

当我仅应用 1 个过滤器时,即根据所选类别过滤用户或根据区域过滤用户,我会得到正确的过滤结果。

现在我需要根据类别和区域选择来过滤用户。 我应该怎样做才能得到想要的结果?

下面是我根据所选类别过滤用户的工作代码:

    User.find().populate('city').populate( {path: 'categories', match: { name: { $in: filterCatArray }}} ).populate('area').exec(function (err, users) {

         users = users.filter(function(user) {
                return user.categories; // return only filtered users 
           });

         data = {fulldata:users}; 
         res.render('home',{data:data});

    });

用户架构:

      var userSchema = new Schema({
      city: {type: mongoose.Schema.Types.ObjectId, ref: 'City',required: true},
      area: {type: mongoose.Schema.Types.ObjectId, ref: 'Area', required: true},
      categories: [{type: mongoose.Schema.Types.ObjectId, ref: 'Category', required: true}],
      status: { type: Boolean, default: true },
      created_at: Date,
      updated_at: Date
    });

区域架构

    var areaSchema = new Schema({
      city: {type: mongoose.Schema.Types.ObjectId, ref: 'City'},
      name: { type: String, required: true},
      status: { type: Boolean, default: true },
      created_at: Date,
      updated_at: Date
    });

类别架构

    var catSchema = new Schema({
      name: { type: String, required: true},
      status: { type: Boolean, default: true },
      created_at: Date,
      updated_at: Date
    });

城市架构

    var citySchema = new Schema({
      name: { type: String, required: true},
      status: { type: Boolean, default: true },
      created_at: Date,
      updated_at: Date
    });

最佳答案

我是 aggregation 的新手,但我最近开始使用它。我认为 populate 非常适合简单的情况,但是当您想要开始执行复杂的操作(例如根据其他集合过滤结果)时,您需要使用聚合。

这未经测试,但看起来大致如下:

UserSchema.aggregate([
    // 1 join city collection
    {
        $lookup: {
            from: 'citys',
            localField: 'city',
            foreignField: '_id',
            as: 'city'
        }
    },
    {
        $unwind: '$city'
    },
    // 2 join area collection
    {
        $lookup: {
            from: 'areas',
            localField: 'area',
            foreignField: '_id',
            as: 'area'
        }
    },
    {
        $unwind: '$area'
    },
    // 3 join categories collection
    {
        $unwind: '$categories'
    },
    {
        $lookup: {
            from: 'categories',
            localField: 'categories',
            foreignField: '_id',
            as: 'category'
        }
    },
    // 4 filter results that only have specified categories
    {
        $match: { 
            'category.name': { $in: filterCatArray },
            'area.name': 'something' // just an example
        }
    },
    // 5 group results by user
    {
        $group: {
            _id: '$_id',
            city: { $first: '$city' },
            area: { $first: '$area' },
            categories: { $push: '$category' },
            status: { $first: '$status' },
            created_at: { $first: '$created_at' },
            updated_at: { $first: '$updated_at' },
        }
    }
], function (err, users) {

});

如果出现问题,请从底部开始注释管道的每个阶段,看看发生了什么。

有关更多信息,请阅读以下链接:

关于node.js - 如何根据填充中的多个条件过滤 Mongoose 中的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44844594/

相关文章:

node.js - Nodejs undefined variable

mongodb - Mongo聚合查询-字符串包含值的过滤条件

node.js - 在 MEAN 堆栈中,如何进行一次性 MongoDB 索引?

node.js - Mongoose 可以为我提供某个领域的所有不同值(value)吗?

javascript - 为什么我必须在 gulp 中使用vinyl-source-stream?

javascript - Heroku Node.js Amazon S3 直接上传教程 - SignatureDoesNotMatch

mysql - 从 Moleculer Framework 中的 mysql 数据库中的 find 方法获取选择字段

node.js - 如何在 Sails 中使用 http-auth

mongodb - 我怎样才能处理一个非常大的数据库而不错过性能?

node.js - 对与删除和放置关联的端点感到困惑