【问题标题】:MongoDB Query for Records Where Value Occurs N Times or MoreMongoDB查询值出现N次或更多的记录
【发布时间】:2020-01-08 13:39:55
【问题描述】:

哪个 MongoDB 查询会返回 Team 记录,其中一个城市在集合中至少出现两次并且它们处于活动状态?

如果你只能回答第一部分也有帮助。 (返回该城市至少出现两次的记录)

团队

[
  {teamName: 'Red', city: 'Altanta', active: false},
  {teamName: 'Blue', city: 'NYC', active: true},
  {teamName:'Yellow', city: 'NYC', active: false},
  {teamName:'Green', city: 'NYC', active: true},
  {teamName:'Gray', city: 'Atlanta', active: false},
  {teamName:'Purple', city: 'Atlanta', active: true},
  {teamName:'Black', city: 'Boston', active: false},
  {teamName:'Brown', city: 'Boston', active: true},
  {teamName:'Silver', city: 'Miami', active: false},
  {teamName:'White', city: 'Austin', active: true},
  {teamName:'Gold', city: 'Detroit', active: true},
]

查询将返回:

[
  {teamName: 'Blue', city: 'NYC', active: true},
  {teamName:'Green', city: 'NYC', active: true},
  {teamName:'Purple', city: 'Atlanta', active: true},
  {teamName:'Brown', city: 'Boston', active: true},
]

【问题讨论】:

  • 您是否必须首先检查active:true 或首先获取城市重复的文件然后检查活动?因为如果我们首先检查活动,那么您将看不到 Atlanta Boston

标签: mongodb mongoose mongodb-query


【解决方案1】:

请试试这个:

查询 1:

Teams.aggregate([{ $group: { _id: '$city', count: { $sum: 1 }, data: { $push: '$$ROOT' } } },
{ $match: { count: { $gte: 2 } } },
// Will project data where docs has active:true & data will be [] if none are active
{
    $project: {
        data: {
            $filter: {
                input: '$data',
                as: "item",
                cond: { $eq: ["$$item.active", true] }
            }
        }
    }
},
// Will remove docs where city is repeated but all documents has active:false
{ $match: { data: { $ne: [] } } }]) 

结果:

/* 1 */
{
    "_id" : "NYC",
    "data" : [ 
        {
            "_id" : ObjectId("5e156407c952870d7dfbb227"),
            "teamName" : "Blue",
            "city" : "NYC",
            "active" : true
        }, 
        {
            "_id" : ObjectId("5e156407c952870d7dfbb229"),
            "teamName" : "Green",
            "city" : "NYC",
            "active" : true
        }
    ]
}

/* 2 */
{
    "_id" : "Atlanta",
    "data" : [ 
        {
            "_id" : ObjectId("5e156407c952870d7dfbb22b"),
            "teamName" : "Purple",
            "city" : "Atlanta",
            "active" : true
        }
    ]
}

/* 3 */
{
    "_id" : "Boston",
    "data" : [ 
        {
            "_id" : ObjectId("5e156407c952870d7dfbb22d"),
            "teamName" : "Brown",
            "city" : "Boston",
            "active" : true
        }
    ]
}

查询 2:

Teams.aggregate([
    { $match: { active: true } },
    { $group: { _id: '$city', count: { $sum: 1 }, data: { $push: '$$ROOT' } } },
    { $match: { count: { $gte: 2 } } }, { $project: { count: 0 } }])

结果:

/* 1 */
{
    "_id" : "NYC",
    "data" : [ 
        {
            "_id" : ObjectId("5e156407c952870d7dfbb227"),
            "teamName" : "Blue",
            "city" : "NYC",
            "active" : true
        }, 
        {
            "_id" : ObjectId("5e156407c952870d7dfbb229"),
            "teamName" : "Green",
            "city" : "NYC",
            "active" : true
        }
    ]
}

Query1Query2 之间的差异就像在第二个查询中过滤掉 active:false 那样,那么文档会更少,因此城市重复也会受到影响,因为只有更少的文档(您可以在query1 结果中看到AtlantaBoston 出现在结果中,尽管它们在data 中有单个文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 2014-04-24
    相关资源
    最近更新 更多