撤消mongodb中的合计撤消 [英] Undo Unwind in aggregate in mongodb

查看:48
本文介绍了撤消mongodb中的合计撤消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个类似这样的数据

I have multiple data something like this

{
    "_id" : ObjectId("57189fcd72b6e0480ed7a0a9"),
    "venueId" : ObjectId("56ce9ead08daba400d14edc9"),
    "companyId" : ObjectId("56e7d62ecc0b8fc812b2aac5"),
    "cardTypeId" : ObjectId("56cea8acd82cd11004ee67a9"),
    "matchData" : [ 
        {
            "matchId" : ObjectId("57175c25561d87001e666d12"),
            "matchDate" : ISODate("2016-04-08T18:30:00.000Z"),
            "matchTime" : "20:00:00",
            "_id" : ObjectId("57189fcd72b6e0480ed7a0ab"),
            "active" : 3,
            "cancelled" : 0,
            "produced" : 3
        }, 
        {
            "matchId" : ObjectId("57175c25561d87001e666d13"),
            "matchDate" : ISODate("2016-04-09T18:30:00.000Z"),
            "matchTime" : "20:00:00",
            "_id" : ObjectId("57189fcd72b6e0480ed7a0aa"),
            "active" : null,
            "cancelled" : null,
            "produced" : null
        }
    ],
    "__v" : 0
}

我正在按 companyId 分组并且工作正常,但是我想基于 matchtime matchId 在 matchData 中进行搜索code>为此,我使用这样的搜索查询在展开后为 $ unwind matchData

i m doing group by companyId and its work fine But i want to search in matchData based on matchtime and matchId For that purpose i am $unwind matchData after unwind i using my search query like this

db.getCollection('matchWiseData').aggregate([
{"$match":{
   "matchData.matchId":{"$in":[ObjectId("57175c25561d87001e666d12")]}
}},
{"$unwind":"$matchData"},
{"$match":{
    "matchData.matchId":{"$in":[ObjectId("57175c25561d87001e666d12")]}}
}])

它给我适当的结果,但是在应用unwind之后,有什么方法可以撤消它,我使用unwind只是在子文档内部进行搜索,或者有其他方法可以在子文档内部进行搜索.

its give me proper result but after applying unwind is there any way to undo it I m using unwind to just search inside subdocument or there is any other way to search inside subdocument.

推荐答案

您当然可以使用 $ push > $ first $ group 中获取文档恢复原样:

Well you can of course just use $push and $first in a $group to get the document back to what it was:

db.getCollection('matchWiseData').aggregate([
    { "$match":{
       "matchData.matchId":{"$in":[ObjectId("57175c25561d87001e666d12")]}
    }},
    { "$unwind":"$matchData"},
    { "$match":{
        "matchData.matchId":{"$in":[ObjectId("57175c25561d87001e666d12")]}
    }},
    { "$group": {
        "_id": "$_id",
        "venueId": { "$first": "$venueId" },
        "companyId": { "$first": "$companyId" },
        "cardTypeId": { "$first": "$cardTypeId" },
        "matchData": { "$push": "$matchData" }
    }}
])

但是您可能应该只使用 $ filter 首先是MongoDB 3.2:

But you probably should have just used $filter with MongoDB 3.2 in the first place:

db.getCollection('matchWiseData').aggregate([
    { "$match":{
       "matchData.matchId":{"$in":[ObjectId("57175c25561d87001e666d12")]}
    }},
    { "$project": {
        "venueId": 1,
        "companyId": 1,
        "cardTypeId": 1,
        "matchData": { 
            "$filter": {
                "input": "$matchData",
                "as": "match",
                "cond": {
                   "$or": [
                       { "$eq": [ "$$match.matchId", ObjectId("57175c25561d87001e666d12") ] }
                   ]
                }
            }
        }
    }}
])

如果您至少拥有MongoDB 2.6,您仍然可以使用 $ map $ setDifference 代替:

And if you had at least MongoDB 2.6, you still could have used $map and $setDifference instead:

db.getCollection('matchWiseData').aggregate([
    { "$match":{
       "matchData.matchId":{"$in":[ObjectId("57175c25561d87001e666d12")]}
    }},
    { "$project": {
        "venueId": 1,
        "companyId": 1,
        "cardTypeId": 1,
        "matchData": { 
            "$setDifference": [
                { "$map": {
                    "input": "$matchData",
                    "as": "match",
                    "in": {
                        "$cond": [
                           { "$or": [
                              { "$eq": [ "$$match.matchId", ObjectId("57175c25561d87001e666d12") ] }
                           ]},
                            "$$match",
                            false
                        ]
                    }
                }},
                [false]
            ]
        }
    }}
])

当每个数组元素已经具有一个唯一"标识符时,这很好,因此设置"操作只是从 $ map 中删除 false 值.

That's perfectly fine when every array element already has a "unique" identifier, so the "set" operation just removes the false values from $map.

这两种方法都可以从数组中过滤"内容而无需实际使用 $ unwind

Both of those a ways to "filter" content from an array without actually using $unwind

笔记本电脑:不确定您是否真的掌握了 $ in 用于匹配条件列表" ,而不是要求与数组匹配.因此,一般情况可以是:

N.B: Not sure if you really grasp that $in is used to match a "list of conditions" rather than being required to match on arrays. So generally the condition can just be:

 "matchData.matchId": ObjectId("57175c25561d87001e666d12")

实际上只有一个要匹配的值.当您具有条件列表"时,可以使用 $ in $ or .数组本身对所需的运算符没有影响.

Where you only actually have a single value to match on. You use $in and $or when you have a "list" of conditions. Arrays themselves make no difference to the operator required.

这篇关于撤消mongodb中的合计撤消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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