问题
I want the following query below to only return those sub documents from the empActivity array where the Stamp field in every sub document matches the Stamp value in the query. empId and empActivity are the outer level fields with empActivity having embedded documents.
db.emp_activity.find({$and : [{"empId" : "999"}, {"empActivity.Stamp" : { $lte : ISODate("2015-01-09T12:33:39.927Z")}}]})
The problem is that it also returns all the sub documents that dont match the date in the query, apart from the 4 sub documents having a date of 9th Jan 2015, the query above also returns sub documents whose date is greater than 9th Jan, 2015.
回答1:
The desired output could be produce just by aggregation:
db.collection.aggregate([
{$match : { empId : '999', 'empActivity.Stamp' : { $lte : ISODate("2015-01-09T12:33:39.927Z")} }},
{$unwind : '$empActivity'},
{$match : { empId : '999', 'empActivity.Stamp' : { $lte : ISODate("2015-01-09T12:33:39.927Z")} }},
{$group: { _id: '$empId', empActivity: { $addToSet: '$empActivity' }}}
])
in c#:
var args = new AggregateArgs
{
Pipeline = new List<BsonDocument>
{
BsonDocument.Parse("{$match : { empId : '999', 'empActivity.Stamp' : { $lte : ISODate('2015-01-09T12:33:39.927Z')} }}"),
BsonDocument.Parse("{$unwind : '$empActivity'}"),
BsonDocument.Parse("{$match : { empId : '999', 'empActivity.Stamp' : { $lte : ISODate('2015-01-09T12:33:39.927Z')} }}"),
BsonDocument.Parse("{$group: { _id: '$empId', empActivity: { $addToSet: '$empActivity' }}}"),
}
};
var result = collection.Aggregate(args).ToList();
来源:https://stackoverflow.com/questions/27862389/mongodb-retrieve-only-matching-sub-documents-from-a-document-with-c-sharp