[SERVER-33582] Add $bsonSize expression Created: 01/Mar/18  Updated: 29/Oct/23  Resolved: 12/Dec/19

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: 4.3.3

Type: New Feature Priority: Major - P3
Reporter: Asya Kamsky Assignee: David Percy
Resolution: Fixed Votes: 9
Labels: asya, expression, pm1457
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Documented
is documented by DOCS-13183 Investigate changes in SERVER-33582: ... Closed
Related
is related to SERVER-7549 document level stats Backlog
is related to SERVER-30967 Add $binarySize expression Closed
Backwards Compatibility: Fully Compatible
Sprint: Query 2019-12-16
Participants:

 Description   

The query team has discussed and agreed to the following syntax and semantics:
Name: $bsonSize
Rejected alternative names: $bsonElementSize, $bsonObjSize
This expression takes an expression that evaluates to an object, and return the size of the object in bytes when encoded as BSON. The resulting size excludes any metadata in the document. Errors on non-nullish non-objects. If the expression is nullish, returns null.
Syntax:

{ $bsonSize: <Expression> } 

This will parse like all other unary expressions, and accept the argument by itself or as an array with one element.

Original Ticket Description

Twice in the last week someone asked how to compute object size (in bytes) in aggregation.

Once it was when they were trying to rewrite mapReduce job in agg (mapReduce has access to Object.bsonsize() method) and the other time when they were trying to compute in aggregation average size of certain oplog entries.

Seems like it could be useful and simple enough if it does exactly what Object.bsonsize() method does. Possible syntax: {$objSize:<expression-resolving-to-object>}. Alternative may be overloaded $bsonSize expression which takes any type and returns its size in bytes (usable for scalar types, arrays, objects, etc)



 Comments   
Comment by Githook User [ 12/Dec/19 ]

Author:

{'name': 'David Percy', 'email': 'david.percy@mongodb.com', 'username': 'dpercy'}

Message: SERVER-33582 Add $bsonSize expression
Branch: master
https://github.com/mongodb/mongo/commit/82fb9653c610121a770ebe5988b3ef3a3e19b6bd

Comment by Asya Kamsky [ 01/Mar/18 ]

If it handled any type, it would be a solution for SERVER-30967.

Also then we could get rid of $strLenBytes and rename/alias $strLenCP to just $strLen.

Generated at Thu Feb 08 04:33:54 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.