查找名称"Chris"的 MongoDB 集合大小
mongodbdatabasebig data analytics更新于 2026/1/10 13:52:17
为此,请在 MongoDB 中使用 bsonsize()。让我们创建一个包含文档的集合 −
> db.demo250.insertOne({"Name":"Chris"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e46bd501627c0c63e7dba70")
}
> db.demo250.insertOne({"Name":"Bob"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e46bd531627c0c63e7dba71")
}
> db.demo250.insertOne({"Name":"David"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e46bd561627c0c63e7dba72")
}
> db.demo250.insertOne({"Name":"Chris"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e46bd5b1627c0c63e7dba73")
}
借助 find() 方法显示集合中的所有文档 −
> db.demo250.find();
这将产生以下输出 −
{ "_id" : ObjectId("5e46bd501627c0c63e7dba70"), "Name" : "Chris" }
{ "_id" : ObjectId("5e46bd531627c0c63e7dba71"), "Name" : "Bob" }
{ "_id" : ObjectId("5e46bd561627c0c63e7dba72"), "Name" : "David" }
{ "_id" : ObjectId("5e46bd5b1627c0c63e7dba73"), "Name" : "Chris" }
以下查询用于查找名称"Chris"的 MongoDB 集合大小 −
> var t = 0;
> db.demo250.find({Name:"Chris"}).forEach(function(doc) {
... var s = Object.bsonsize(doc);
... t += s;
... })
> print(t);
This will produce the following output in bytes −
76

