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

标签 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") }

如果文档的 TempId 与整数列表匹配,您将如何按 PersonId 对所有文档进行分组 - 并且最终输出不会是

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 BsonDocument的List:

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 组成,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"}]

谢谢!

最佳答案

您可以按如下方式创建 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# Mongodb 将 List<BsonDocument> 合并为单个 BsonArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43670715/

相关文章:

python - 如何按键排序json文件

node.js - Webpack/babel 意外 token ,预期为 ";"

java - Spring data mongodb repository findAll字段排除

javascript - 获取 template.foo.rendered 中集合实例的 this._id

c# - 下载dotnet运行时v7.0.9时VSCode超时错误

c# - 如何从 jQuery ajax 调用将复杂对象传递给 ASP.NET WebApi GET?

javascript - 如何引用json的属性

node.js - 如何在nodejs中上传和读取excel文件?

c# - ContinueWith() 直到主机进程结束才执行

c# - 大型列表上的 plinq 需要花费大量时间