sql >> Database teknologi >  >> NoSQL >> MongoDB

Kort Reducer type forespørgsel med behov for at korrelere med forrige række

Hvis du gjorde dette i relationsdatabasen, ville du ikke sammenligne besøg række for række, i stedet ville du bruge en aggregeringsforespørgsel til at finde gentagne besøg (ved at bruge SELECT ... GROUP BY), så du bør gøre det på samme måde i MongoDB.

Først skal du samle besøg pr. kunde pr. butik pr. dag:

group1 = { "$group" : {
        "_id" : {
            "c" : "$clientId",
            "l" : "$location",
            "day" : {
                "y" : {
                    "$year" : "$tov"
                },
                "m" : {
                    "$month" : "$tov"
                },
                "d" : {
                    "$dayOfMonth" : "$tov"
                }
            }
        },
        "visits" : {
            "$sum" : 1
        }
    }
};

REDIGER da du kun ønsker at gentage DAYS næste gang, vil du gruppere efter kunde, efter butik og tælle, hvor mange forskellige DAYS der var for besøg fra den pågældende kunde i den butik:

group2 = {"$group" : 
    {"_id" : {
        "c" : "$_id.c",
        "s" : "$_id.l"
    },
    "totalDays" : {
        "$sum" : 1
    }
} };

Så vil du kun inkludere registreringerne fra oven, hvor der var mere end ét besøg af den samme kunde i den samme butik over flere dage:

match = { "$match" : { "totalDays" : { "$gt" : 1 } } };

Her er et eksempel på datasæt og resultatet af disse sammenlægninger ved hjælp af ovenstående pipeline-operationer:

> db.visits.find({},{_id:0,purchases:0}).sort({location:1, clientId:1, tov:1})
{ "clientId" : 1, "location" : "l1", "tov" : ISODate("2013-01-01T20:00:00Z") }
{ "clientId" : 1, "location" : "l1", "tov" : ISODate("2013-01-01T21:00:00Z") }
{ "clientId" : 1, "location" : "l1", "tov" : ISODate("2013-01-03T20:00:00Z") }
{ "clientId" : 2, "location" : "l1", "tov" : ISODate("2013-01-01T21:00:00Z") }
{ "clientId" : 3, "location" : "l1", "tov" : ISODate("2013-01-01T21:00:00Z") }
{ "clientId" : 3, "location" : "l1", "tov" : ISODate("2013-01-02T21:00:00Z") }
{ "clientId" : 1, "location" : "l2", "tov" : ISODate("2013-01-01T23:00:00Z") }
{ "clientId" : 3, "location" : "l2", "tov" : ISODate("2013-01-02T21:00:00Z") }
{ "clientId" : 3, "location" : "l2", "tov" : ISODate("2013-01-02T21:00:00Z") }
{ "clientId" : 1, "location" : "l3", "tov" : ISODate("2013-01-03T20:00:00Z") }
{ "clientId" : 2, "location" : "l3", "tov" : ISODate("2013-01-04T20:00:00Z") }
{ "clientId" : 4, "location" : "l3", "tov" : ISODate("2013-01-04T20:00:00Z") }
{ "clientId" : 4, "location" : "l3", "tov" : ISODate("2013-01-04T21:00:00Z") }
{ "clientId" : 4, "location" : "l3", "tov" : ISODate("2013-01-04T22:00:00Z") }

> db.visits.aggregate(group1, group2, match)
{
    "result" : [
    {
        "_id" : {
            "c" : 3,
            "s" : "l1"
        },
        "totalDays" : 2
    },
    {
        "_id" : {
            "c" : 1,
            "s" : "l1"
        },
        "totalDays" : 2
    }
    ],
    "ok" : 1
}



  1. mongodb opdatere hvis et felt ikke eksisterer

  2. Redis serialisering og deserialisering

  3. Hvordan håndterer MongoDB samtidige opdateringer?

  4. MongoDB $minut