25

我有 mongo 查询,它对文档进行组操作。

除了我想在没有空值或空值的情况下优化结果之外,我几乎得到了预期的结果。

目前我的查询如下所示:

db.productMetadata.aggregate([{$group:{"_id":{"color":"$productAttribute.colour","gender":"$productAttribute.gender"},"count" : {$sum : 1}}}]);

结果看起来像这样:

{ "_id" : { "color" : "BLUE", "gender" : "MEN" }, "count" : 1 }
{ "_id" : {  }, "count" : 4 }
{ "_id" : { "color" : "NA", "gender" : "WOMEN" }, "count" : 1 }
{ "_id" : { "color" : "BLACK", "gender" : "MEN" }, "count" : 1 }
{ "_id" : { "color" : "BROWN", "gender" : "WOMEN" }, "count" : 1 }
{ "_id" : { "gender" : "MEN" }, "count" : 2 }
{ "_id" : { "color" : "BEIGE", "gender" : "MEN" }, "count" : 1 }
{ "_id" : { "color" : "BROWN", "gender" : "MEN" }, "count" : 1 }

如果 DB 的实际数据中的任何 group by 字段值为空或 null,我想删除这些行。

异常结果应如下所示:

{ "_id" : { "color" : "BLUE", "gender" : "MEN" }, "count" : 1 }
{ "_id" : { "color" : "NA", "gender" : "WOMEN" }, "count" : 1 }
{ "_id" : { "color" : "BLACK", "gender" : "MEN" }, "count" : 1 }
{ "_id" : { "color" : "BROWN", "gender" : "WOMEN" }, "count" : 1 }
{ "_id" : { "color" : "BEIGE", "gender" : "MEN" }, "count" : 1 }
{ "_id" : { "color" : "BROWN", "gender" : "MEN" }, "count" : 1 }
4

3 回答 3

51

您需要一个额外的$match管道步骤,该步骤将根据"$productAttribute.colour"现有的嵌入字段而不是 null 过滤传入的文档:

    db.productMetadata.aggregate([
    { 
        "$match": {
            "productAttribute.colour": { 
                "$exists": true, 
                "$ne": null 
            }
        }    
    },
    { 
        "$group": {
            "_id": {
                "color": "$productAttribute.colour",
                "gender": "$productAttribute.gender"
            },
            "count": { 
                "$sum": 1 
            }
        }   
    }        
]);
于 2015-10-14T11:13:27.100 回答
1

此示例包括两个不同的集合。为此,我们使用聚合函数。我也在使用猫鼬

  1. 我正在使用带有 $lookup 的 customfiellabels 加入 cusmtomfield
  2. 使用 $unwind 平展阵列
  3. $match 以排除文本中具有 INACTIVE 的名称(我使用的是 REGEX)
  4. $project 重命名字段以在客户端上正确显示

    . 异步 getAllMasterDataCustomFields(req) {

        let response = {};
        try {
    
          response = await customfieldsModel.aggregate([
            {
              $lookup: {
                from: 'customfieldlabels',
                localField: 'cfId',
                foreignField: 'cfId',
                as: 'info'
              }
            },
            { '$unwind': { 'path': '$info', 'preserveNullAndEmptyArrays': true } },
            { '$match': { 'childs.name': { $not: /INACTIVE/ }}},
            {
              $project: {
                'cfId': 1,
                'label': '$info.label',
                'type': '$info.type',
                'childs': 1
              }
            }]).exec();
    
        } catch (e) {
          logger.log('error', `Error while getting response ${e.meesage}`);
        }
    
        return response;
      }
    

    .

于 2019-05-22T19:55:08.487 回答
0

也许你应该在 $group 操作之前使用 $match: {'color': {$exists: true}} 。使用稀疏索引,它将工作得非常快。并且根本不要在集合中存储“空”字段,这将减少数据库大小并提高稀疏索引的搜索速度(索引中的文档更少 -> 速度更快)

于 2015-10-14T11:17:30.410 回答