MongoDB
 sql >> Datenbank >  >> NoSQL >> MongoDB

Karte Reduzieren Sie die Art der Abfrage, die mit der vorherigen Zeile korrelieren muss

Wenn Sie dies in einer relationalen Datenbank tun würden, würden Sie die Besuche nicht Zeile für Zeile vergleichen, sondern Sie würden eine Aggregationsabfrage verwenden, um wiederholte Besuche zu finden (mit SELECT ... GROUP BY), also sollten Sie es in MongoDB genauso machen.

Zuerst müssen Sie die Besuche pro Kunde pro Geschäft und Tag aggregieren:

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

BEARBEITEN Da Sie nur TAGE wiederholen möchten, würden Sie als Nächstes nach Kunde und Geschäft gruppieren und zählen, wie viele verschiedene TAGE es für Besuche dieses Kunden in diesem Geschäft gab:

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

Dann möchten Sie nur die Datensätze von oben einbeziehen, wenn derselbe Kunde an mehreren Tagen mehr als einen Besuch in demselben Geschäft verzeichnete:

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

Hier ist ein Beispieldatensatz und das Ergebnis dieser Aggregationen unter Verwendung der obigen Pipeline-Operationen:

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