Ich bin neu in MongoDB und ich versuche mit Aggregationen zu arbeiten. Ich tue teilweise, was ich suche, aber ich habe ein seltsames Verhalten mit Daten.
MongoDB info
Version: 2.2.0
Betriebssystem: Windows 7
Ziel
erhalten alle Kommentare erstellt achtern er '2012.11.22'
Nehmen wir ein Beispiel erhalten:
Daten
db.blogs.save([ {
title : "X this is my second title",
author : "max",
posted : new Date(),
pageViews : 10,
tags : [ "good", "nice" ],
comments : [ {
"_id" : ObjectId("50ac9fdb53a900bcb4be46d9"),
author : "john",
text : "pretty awesome",
create : ISODate("2012-12-20T00:00:00.000Z")
}, {
"_id" : ObjectId("50ac9fd003a900bcb4be46d9"),
author : "sam",
text : "this is bad",
create : ISODate("2012-12-22T00:00:00.000Z")
} ],
other : {
foo : 5
}
}, {
title : "X this is my title",
author : "bob",
posted : new Date(),
pageViews : 5,
tags : [ "fun", "good", "fun" ],
comments : [ {
"_id" : ObjectId("50ac55db53a900bcb4be46d9"),
author : "matthieu",
text : "bof bof",
create : ISODate("2012-12-21T00:00:00.000Z")
}, {
"_id" : ObjectId("50ac55db53a900bcb4b226d9"),
author : "sam",
text : "this s bad",
create : ISODate("2012-12-22T00:00:00.000Z")
} ],
other : {
foo : 6
}
}, {
title : "X NEW ELEMENT",
author : "emil",
posted : new Date(),
pageViews : 33,
tags : [ "bad", "hehe", "cool", "nice" ],
comments : [ {
"_id" : ObjectId("50ac55db531100bcb4b226d9"),
author : "emilie",
text : "could be better",
create : ISODate("2012-12-21T00:00:00.000Z")
}, {
"_id" : ObjectId("50ac55db101100bcb4b226d9"),
author : "samuel",
text : "maybe a good one",
create : ISODate("2012-12-20T00:00:00.000Z")
} ],
other : {
foo : 9
}
}, {
title : "X Y NEW ELEMENT",
author : "marc",
posted : new Date(),
pageViews : 33,
tags : [ "bad", "hehe", "cool", "nice" ],
comments : [ {
"_id" : ObjectId("50ac55db101100bcb4baa6d9"),
author : "sam",
text : "hehe",
create : ISODate("2012-11-20T00:00:00.000Z")
}, {
"_id" : ObjectId("50ac55db101ab0bcb4baa6d9"),
author : "daniel",
text : "yeehhhh hoho",
create : ISODate("2012-11-23T00:00:00.000Z")
} ],
other : {
foo : 9
}
} ])
Beispiel 1: OK mit Streichern
Return matching alle 'Kommentare' vom Benutzer 'sam':
db.blogs.aggregate([
{ $unwind: "$comments" },
{ $match: { 'comments.author' : "sam" } },
{ $group: { _id: "$comments" } }
])
Diese Rückkehr nur Kommentare, wo die Eigenschaft 'Autor' ist 'sam'.
Beispiel 2: Ausgabe mit Datumsangaben?
diese Aggregation ist (für mich) die gleiche wie die vorherigen, sondern passenden ‚Autor‘ ich das Datum Eigenschaft übereinstimmen ‚create‘:
db.blogs.aggregate([
{ $unwind: "$comments" },
{ $match: {
'comments.create' : {
$gt: ISODate("2012-11-22T00:00:00Z")
}
} },
{ $group: { _id: "$comments" } }
])
Aber wenn Sie diese Aggregation testen Sie wird sehen, dass einige Kommentare 'create' Daten niedriger als '2012-11-22' enthalten. Zum Beispiel wird ein Kommentar mit der ID '50ac9fdb53a900bcb4be46d9' zurückgegeben.
würde ich nur Kommentare mit Daten von mehr als ‚2012.11.22‘ erwarten ... Ich glaube, ich etwas verpasst ...
Danke
Basierend auf Ihrem Beispielcode hat der Kommentar mit der ID '50ac9fdb53a900bcb4be46d9' ein Datum im Dezember, nicht November: 'create: ISODate (" 2012-12-20T00: 00: 00.000Z ")'. Sieht so aus als ob alles wie geplant funktioniert ;-). – Stennie
BTW, Gruppierung auf "$ comments" ist wahrscheinlich nicht das, was Sie hier tun möchten; Versuchen Sie es mit einer Operation ['$ project'] (http://docs.mongodb.org/manual/reference/aggregation/project/#_S_project) in Ihrer Pipeline, um zu steuern, was in Ihren Ergebnissen enthalten ist. – JohnnyHK