如何在mongodb中获取文档字段的大小(以字节为单位)



我有文档集合(products_entity(。

ID | TYPE | CONTENT_DATA

我可以通过type查询db.getCollection('products_entity').find({type: "comment"})来获取我的文档。

我现在想确定从查询db.getCollection('products_entity').find({type: "comment"})返回的所有CONTENT_DATA字段的大小。

这是可能的,我该怎么做?

或者我应该创建一个名为size的额外列,在创建/更新时填充它并获得这些字段的总和吗?

谢谢!

您可以使用$strLenBytes或$strLenCP

db.products_entity.aggregate([
{
$addFields: {
size: {
$strLenBytes: "$CONTENT_DATA"
}
}
}
])

MongoPlayground

相关内容

最新更新