mongo中的组不包括零值

发布于 2025-01-21 09:33:38 字数 1246 浏览 0 评论 0 原文

我有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的实际数据中,我想删除行。

例外的结果应该看起来像这样:

{ "_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 }

I have mongo query which does the group operation on the documents.

I have almost got the expected results except that I want to refine the results without empty or null values.

Currently my query looks like this:

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

And the results looks something like this:

{ "_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 }

I want to remove the rows if any of the group by field values are empty or null in the actual data of DB.

Excepted results should look something like this:

{ "_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 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱殇璃 2025-01-28 09:33:38

您需要一个额外的“ $ productattribute.colour” 现有而不是null的步骤,而不是null:

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

You need an extra $match pipeline step that will filter the incoming documents based on the embedded field "$productAttribute.colour" existing and not null:

db.productMetadata.aggregate([
    { $match: {
        "productAttribute.colour": { 
            $exists: true, 
            $ne: null 
        }
    } },
    { $group: {
        _id: {
           color: "$productAttribute.colour",
           gender: "$productAttribute.gender"
        },
        count: { $sum: 1 }
    } }        
]);
沙与沫 2025-01-28 09:33:38

此示例包括两个不同的集合。为此,我们使用聚合功能。我还在使用Mongoose

  1. 我正在使用customfiellabels加入cusmtomfield,并带有$ lookup
  2. flat the Array with $ univind
  3. $匹配,以排除文本中没有活动的名称(我正在使用Regex)
  4. $ $ project to重命名字段以正确显示在客户端


    异步GetAllmasterDataCustomFields(req){

     让响应= {};
        尝试 {
    
          响应=等待CustomFieldsModel.Aggregate([[
            {
              $查找:{
                来自:“ CustomFieldLabels”,
                Localfield:“ CFID”,
                外国菲尔德:“ CFID”,
                AS:'info'
              }
            },,
            {'$ undind':{'path':'$ info','preservenullandemptyarrays':true}},
            {'$ match':{'childs.name':{$ not: / intactive /}}}},
            {
              $项目:{
                'cfid':1,
                'label':'$ info.label',
                'type':'$ info.type',
                “孩子”:1
              }
            }])。exec();
    
        }捕获(e){
          logger.log('error',`error在获得响应$ {e.meesage}`时);
        }
    
        返回响应;
      }
     

this example includes two different Collections. For this we use aggregate function. I am also using Mongoose

  1. I am joining the cusmtomfield with customfiellabels with $lookup
  2. Flat the array with $unwind
  3. $match to exclude the name that have INACTIVE in the text (I'm using REGEX)
  4. $project to rename the Fields to show properly on the client

    .
    async 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;
      }
    

    .

予囚 2025-01-28 09:33:38

也许您应该使用$ match:{'color':{$存在:true}} $ group操作之前。使用稀疏索引,它将很快起作用。
并且不要将“ null”字段存储在集合中,这将降低数据库的大小,并会提高稀疏索引(索引中的文档更少 - >更速度)

Perhaps you should use $match: {'color': {$exists: true}} before $group operation. With sparse index it will work pretty fast.
And do not store "null" fields in collections at all, that will reduce db size and will increase search speed for sparse indexes (less documents in index -> more speed)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文