C#Mongodb将List <bsondocument>合并为单个BsonArray

时间:2017-04-28 01:58:06

标签: c# json mongodb linq

这是一些示例mongodb json来说明问题:

{ "_id" : ObjectId("59027ac2c902b324f6efe756"), "PersonId" : "825c47da-4498-4b99-0002-08d3e7e9e8cb", "Items" : [ { "Ver" : "\r\nMicrosoft Windows [Version 10.0.14393]\r\n" } ], "TempId" : 4, "LastUpdated" : ISODate("2017-04-27T23:12:14.365Z") }
{ "_id" : ObjectId("59027ac2c902b324f6efe757"), "PersonId" : "825c47da-4498-4b99-0003-08d3e7e9e8cb", "Items" : [ { "Ver" : "\r\nMicrosoft Windows [Version 6.3.9600]\r\n" } ], "TempId" : 4, "LastUpdated" : ISODate("2017-04-27T23:12:14.338Z") }
{ "_id" : ObjectId("59027acec902b324f6efe75f"), "PersonId" : "825c47da-4498-4b99-8ba7-08d3e7e9e8cb", "Items" : [ { "Ver" : "\r\nMicrosoft Windows [Version 10.0.14393]\r\n" } ], "TempId" : 4, "LastUpdated" : ISODate("2017-04-27T23:12:14.278Z") }
{ "_id" : ObjectId("59027adcc902b324f6efe76a"), "PersonId" : "825c47da-4498-4b99-0002-08d3e7e9e8cb", "Items" : [ { "ComputerName" : "WIN-DRCM8F16QGG" } ], "TempId" : 6, "LastUpdated" : ISODate("2017-04-27T23:12:28.530Z") }
{ "_id" : ObjectId("59027adcc902b324f6efe76c"), "PersonId" : "825c47da-4498-4b99-0003-08d3e7e9e8cb", "Items" : [ { "ComputerName" : "WIN-1GICMOQD1AI" } ], "TempId" : 6, "LastUpdated" : ISODate("2017-04-27T23:12:28.592Z") }
{ "_id" : ObjectId("59027addc902b324f6efe770"), "PersonId" : "825c47da-4498-4b99-8ba7-08d3e7e9e8cb", "Items" : [ { "ComputerName" : "Server1" } ], "TempId" : 6, "LastUpdated" : ISODate("2017-04-27T23:12:29.540Z") }
{ "_id" : ObjectId("59027ae6c902b324f6efe776"), "PersonId" : "825c47da-4498-4b99-0002-08d3e7e9e8cb", "Items" : [ { "OSVersion" : "Microsoft Windows Server 2016 Standard" } ], "TempId" : 8, "LastUpdated" : ISODate("2017-04-27T23:12:38.018Z") }
{ "_id" : ObjectId("59027ae6c902b324f6efe77a"), "PersonId" : "825c47da-4498-4b99-8ba7-08d3e7e9e8cb", "Items" : [ { "OSVersion" : "Microsoft Windows 10 Enterprise" } ], "TempId" : 8, "LastUpdated" : ISODate("2017-04-27T23:12:38.179Z") }
{ "_id" : ObjectId("59027ae6c902b324f6efe77c"), "PersonId" : "825c47da-4498-4b99-0003-08d3e7e9e8cb", "Items" : [ { "OSVersion" : "Microsoft Windows 8.1 Pro" } ], "TempId" : 8, "LastUpdated" : ISODate("2017-04-27T23:12:38.199Z") }

如何按PersonId对所有文档进行分组,其中文档的TempId与整数列表匹配 - 最终输出不是

List<List<BsonDocument>> but a List<BsonArray>

我们到目前为止的示例代码...这将仅针对与我们的TempId列表匹配的文档过滤我们的集合。

var objects = collection.AsQueryable()
    .Where(p => TempIds.Contains(p.TempId))
    .Where(l => l.LastUpdated > minutes).ToList();

这将采用过滤的对象集,并根据PersonId

将它们分组到数组中
var grouped = objects.GroupBy(o => o.PersonId);

以下可以最终生成List of List BsonDocument:

var obj2 = obj1.Select(t => new
{
    bsondoclist =t.SelectMany(x=>x.Answers.ToList()).ToList()
}).ToList();

List<List<BsonDocument>> bsonDocList = obj2.Select(t => t.bsondoclist.ToList()).ToList();

最终数据需要的是List BsonArray,其中每个BsonArray由BsonValue组成,它们是来自每个嵌套BsonDocument List的数据。

也就是说最终输出的json数据看起来像这样:

List<BsonArray> final data example:

[{"ComputerName":"WIN-DRCM8F16QGG","OSVersion":"Microsoft Windows Server 2016 Standard","Ver":"\r\nMicrosoft Windows [Version 10.0.14393]\r\n"},
{"ComputerName":"WIN-1GICMOQD1AI","OSVersion":"Microsoft Windows 8.1 Pro","Ver":"\r\nMicrosoft Windows [Version 6.3.9600]\r\n"},
{"ComputerName":"Server1","OSVersion":"Microsoft Windows 10 Enterprise","Ver":"\r\nMicrosoft Windows [Version 10.0.14393]\r\n"}]

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以按照以下方式创建BsonArray。

var obj1 = collection.AsQueryable().Where(p => TempIds.Contains(p.TempId)).ToList();
var grouped = obj1.GroupBy(o => o.PersonId);
var obj2 = grouped.Select(g => g.Select(x => x.ToBsonDocument().ToArray()).ToList()).ToList();