sql >> Database >  >> NoSQL >> MongoDB

Mongodb retourneert meerdere subarrayresultaten en sluit andere geretourneerde resultaten uit

Wat je nodig hebt is "aggregation" en de $map en de $setDifference operators

db.collection.aggregate([ 
    { 
        "$match": { 
            "node.-user": "Zulu99", 
            "node.-lat": "-37.6787103" 
        }
    },
    { 
        "$project": { 
            "node": { 
                "$setDifference": [{ 
                    "$map": { 
                        "input": "$node",
                        "as": "n", 
                        "in": { 
                            "$cond": [
                                { "$eq": [ "$$n.-user", "Zulu99" ]}, 
                                "$$n", 
                                false 
                            ]
                        } 
                    }
                }, 
                [false]]
            }
        }
    }
])

Vanaf MongoDB 3.2 kunt u de $filter . gebruiken telefoniste

db.collection.aggregate([
    { 
        "$match":  { 
            "node.-user": "Zulu99",
            "node.-lat": "-37.6787103"
        }
    }, 
    { "$project": {
        "node": {
            "$filter": {
                "input": "$node",
                "as": "n",
                "cond": { "$eq": [ "$$node.n", "Zulu99" ] }
            }
        }
    }}
])

wat oplevert:

{
        "_id" : ObjectId("55acf6187d4c31475417fa62"),
        "node" : [
                {
                        "-id" : "60648717",
                        "-uid" : "46482",
                        "-user" : "Zulu99",
                        "-lat" : "-37.6796337",
                        "-lon" : "144.9220639",
                        "-timestamp" : "2009-12-12T21:29:36Z",
                        "-visible" : "true",
                        "-version" : "2",
                        "-changeset" : "3358816"
                },
                {
                        "-id" : "60648718",
                        "-timestamp" : "2009-12-12T21:29:35Z",
                        "-uid" : "46482",
                        "-version" : "2",
                        "-changeset" : "3358816",
                        "-user" : "Zulu99",
                        "-visible" : "true",
                        "-lat" : "-37.6787103",
                        "-lon" : "144.9224609"
                }
        ]
}


  1. maak snel een voorbeeld-hbase-tabel

  2. Django Channels Error - Kan BACKEND 'asgi_redis.RedisChannelLayer' niet importeren

  3. Retourneer alleen overeenkomende subdocumentelementen binnen een geneste array

  4. Beste manier om afbeeldingen op te slaan in de MERN-stack-webtoepassing