这是一些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);

然后,以下操作最终可以生成列表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();

然而,作为最终数据需要的是一个列表bsonarray,其中每个bsonarray由bsonvalue组成,bsonvalue是每个嵌套bsondocument列表中的数据。
也就是说,最终输出的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"}]

谢谢您!

最佳答案

您可以如下创建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();

关于c# - C#Mongodb将List <BsonDocument>合并到单个BsonArray中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43670715/

10-12 12:37