简体   繁体   中英

C# Mongodb merge List<BsonDocument> into single BsonArray

This is some sample mongodb json to illustrate the problem:

{ "_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") }

How would you group all the documents by the PersonId where the TempId of the documents matches a list of integers - and the final output would not be a

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

Sample code of what we have so far... This will filter our collection for only documents that match our list of TempId's.

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

This will take the filtered set of objects and group them in arrays based on the PersonId

var grouped = objects.GroupBy(o => o.PersonId);

The following can then finally generate a 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();

What is needed as the final data however is a List BsonArray where each BsonArray is made up of BsonValue's which were the data from each nested BsonDocument List.

That is to say the final output json data would look like this:

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"}]

Thank you!

You can create BsonArray as following.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM