MongoDB retrieve only matching sub documents from a document with c#

99封情书 提交于 2020-01-03 02:00:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!