MongoDB查询应返回许多信息 [英] MongoDB query should return many information

查看:114
本文介绍了MongoDB查询应返回许多信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然是MongoDB的初学者,我为使用它创建一个复杂的查询(对我而言)而发疯.

I'm still a beginner with MongoDB and I'm getting crazy to create a complex (for me) query with it.

所以,我的模型是:

{
  email: String,
  name: String,
  orientation: String,
  location: {
    country: String,
    city: String
  },
  contacts: {
    phone: String,
    email: String,
    website: String
  }
}

现在,我创建了这样的查询:

Now I created a query like this:

User.aggregate([
    { 
      $group: { 
        _id: { city: '$location.city', country: '$location.country', orientation: '$orientation' },
        count: { $sum: 1 }
      }
    },
    {
      $group: { 
        _id: '$_id.country',
        count: { $sum: '$count' },
        cities: { 
          $push: { 
            city: '$_id.city', 
            count: '$count'
          }
        },
        orientations: {
            $push: {
                orientation: '$_id.orientation',
                count: '$count'
            }
        }
      }
    }
  ], function(err, results) {
    if (err) return next();
    console.log(JSON.stringify(results));
  });

不幸的是,我收到了很好的数据,但只是一部分.

Unfortunately I receive good data but just in part.

我希望拥有的记录是:

  • 总用户(我没有)
  • 每个国家/地区的用户总数(我有)
  • 每个城市的用户总数(我有)
  • 每个方位的用户总数(我没有,每个城市每个方位的用户总数)
  • 每个城市中每个方向的用户总数(我有)
  • total users (I don't have it)
  • total users in each Country (I have it)
  • total users in each City (I have it)
  • total users for each orientation (I don't have it, I have total users for each orientation for each city)
  • total users for each orientation in each city (I have it)

当前返回:

[{
    "_id": "France",
    "count": 1,
    "cities": [{
        "city": "Maël-Carhaix",
        "count": 1
    }],
    "orientations": [{
        "orientation": "a",
        "count": 1
    }]
}, {
    "_id": "United Kingdom",
    "count": 4,
    "cities": [{
        "city": "Bagshot",
        "count": 1
    }, {
        "city": "London",
        "count": 3
    }],
    "orientations": [{
        "orientation": "a",
        "count": 1
    }, {
        "orientation": "a",
        "count": 3
    }]
}]

这就是我想要的:

"totalUsers": 5,
"countries: [{
    "_id": "France",
    "count": 1,
    "cities": [{
        "city": "Maël-Carhaix",
        "count": 1
    }],
    "orientations": [{
        "orientation": "a",
        "count": 1
    }]
}, {
    "_id": "United Kingdom",
    "count": 4,
    "cities": [{
        "city": "Bagshot",
        "count": 1
    }, {
        "city": "London",
        "count": 3
    }],
    "orientations": [{
        "orientation": "a",
        "count": 4
    }, 
    {
        "orientation": "b" // if exist...
        "count: // n...
    }]
}]

我认为已经对该查询进行了数百次更改,但是我占了歌剧的60%.

I think to have changed this query hundreds of times but I'm at the 60% of the opera.

推荐答案

对于总用户,您可以执行以下操作:

For total users you could do:

db.collection.distinct("email").length

对于每个方位的总用户,您可以执行此操作(假设模型中的计数是唯一身份用户的计数):

For total users for each orientation, you may be able to do this (assuming count in your model is that of unique users):

db.collection.group({
   key: {"orientations.orientation": 1 },
   reduce: function(cur, result) { result.count += cur.count },
   initial: { count: 0 }
})

如尼尔所说,将来最好提供完整的模型(即使您输入了虚假数据也是如此),以便我们可以帮助您并写出切实可行的答案.

In the future, as Niel has already mentioned, it's best to provide the full model (even if you input fake data) so that we can help you and write answers that actually make sense.

这篇关于MongoDB查询应返回许多信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆