[CSHARP-3566] Allow the use of bsonsize in the c# driver Created: 12/Apr/21  Updated: 27/Oct/23  Resolved: 13/Apr/21

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Hugh Walsh Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I am using a multi tenant database and we would like to divide up the costs based on storage.  To find out the amount of data i would like to make use of the bsonsize group operator.

 

With the 

C# code

var match = new BsonDocument
{ {
"$match", new BsonDocument
{

Unknown macro: { "_id.DesignId", new BsonBinaryData(designId, GuidRepresentation.Standard) }

}
} };
var group = new BsonDocument
{ {
"$group", new BsonDocument

Unknown macro: { { "_id", BsonNull.Value }

,
{ "size", new BsonDocument {

Unknown macro: { "$bsonSize", "$$ROOT" }

} },
}
} };

var result = collection.Aggregate(PipelineDefinition<T, BsonDocument>.Create(match, group)).ToEnumerable().ToList();

 

 

I get the error:
MongoDB.Driver.MongoCommandException: Command aggregate failed: unknown group operator '$bsonSize'.

 

 



 Comments   
Comment by Dmitry Lukyanov (Inactive) [ 13/Apr/21 ]

Hello hugh.walsh@romaxtech.com,

since you're working with $group, you should also use $sum (group operator) as it's described here , so the below code should work for you:

var group = new BsonDocument
{
	{
		"$group", new BsonDocument
		{
			{ "_id", BsonNull.Value },
			{ 
				"size", new BsonDocument("$sum", new BsonDocument("$bsonSize", "$$ROOT")) 
			},
		}
	}
};

Comment by Esha Bhargava [ 13/Apr/21 ]

hugh.walsh@romaxtech.com Thank you for reporting this issue! We'll look into it and get back to you soon.

Generated at Wed Feb 07 21:45:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.