sql >> Base de Datos >  >> NoSQL >> MongoDB

Deshacer Deshacer en conjunto en mongodb

Bueno, por supuesto, puedes usar $push y $first en un $group para que el documento vuelva a ser lo que era:

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" }
    }}
])

Pero probablemente debería haber usado $filter con MongoDB 3.2 en primer lugar:

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") ] }
                   ]
                }
            }
        }
    }}
])

Y si tuviera al menos MongoDB 2.6, aún podría haber usado $map y $setDifference en cambio:

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]
            ]
        }
    }}
])

Eso está perfectamente bien cuando cada elemento de la matriz ya tiene un identificador "único", por lo que la operación "establecer" simplemente elimina el false valores de $map .

Ambas son formas de "filtrar" el contenido de una matriz sin usar $unwind

N.B :No estoy seguro si realmente entiendes que $in se utiliza para hacer coincidir una "lista de condiciones" en lugar de tener que hacer coincidir las matrices. Por lo general, la condición puede ser simplemente:

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

Donde en realidad solo tiene un valor único para hacer coincidir. Usas $in y $or cuando tienes una "lista" de condiciones. Los arreglos en sí mismos no hacen ninguna diferencia para el operador requerido.