Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MongoDB

Tìm kích thước collection MongoDB cho tên "Chris"

Để tính kích thước, sử dụng hàm bsonsize() trong MongoDB shell.

Tạo collection:

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

Hiển thị dữ liệu (2 bản ghi Chris):

> db.demo250.find();
{ "_id" : ObjectId("5e46bd501627c0c63e7dba70"), "Name" : "Chris" }
{ "_id" : ObjectId("5e46bd531627c0c63e7dba71"), "Name" : "Bob" }
{ "_id" : ObjectId("5e46bd561627c0c63e7dba72"), "Name" : "David" }
{ "_id" : ObjectId("5e46bd5b1627c0c63e7dba73"), "Name" : "Chris" }

Script tính tổng kích thước BSON của các bản ghi Name="Chris":

> var t = 0;
> db.demo250.find({Name:"Chris"}).forEach(function(doc) {
...   var s = Object.bsonsize(doc);
...   t += s;
... })
> print(t);

Kết quả kích thước tính bằng bytes:

76