要在MongoDB中找到最大的文档大小,您需要在shell中编写脚本。
为了理解这个概念,让我们用文档创建一个集合。使用文档创建集合的查询如下-
> db.largestDocumentDemo.insertOne({"StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ed2e32f684a30fbdfd57d") } > db.largestDocumentDemo.insertOne({"StudentName":"Carol","StudentAge":22,"StudentCountryName":"US","TechnicalSubject":["C","C++","Java","MySQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ed3282f684a30fbdfd57e") } > db.largestDocumentDemo.insertOne({"StudentName":"Mike","StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ed3382f684a30fbdfd57f") }
在find()
method的帮助下显示集合中的所有文档。查询如下-
> db.largestDocumentDemo.find().pretty();
以下是输出-
{ "_id" : ObjectId("5c8ed2e32f684a30fbdfd57d"), "StudentName" : "John" } { "_id" : ObjectId("5c8ed3282f684a30fbdfd57e"), "StudentName" : "Carol", "StudentAge" : 22, "StudentCountryName" : "US", "TechnicalSubject" : [ "C", "C++", "Java", "MySQL" ] } { "_id" : ObjectId("5c8ed3382f684a30fbdfd57f"), "StudentName" : "Mike", "StudentAge" : 22 }
这是在MongoDB中查找最大文档大小的查询-
> var largestDocumentSize = 0; > db.largestDocumentDemo.find().forEach(function(myObject) { ... var currentDocumentSize = Object.bsonsize(myObject); ... if(largestDocumentSize < currentDocumentSize ) { ... largestDocumentSize = currentDocumentSize ; ... } ... }); > print('The Largest Document Size =' + ' ' +largestDocumentSize);
以下是显示最大文档大小的输出-
The Largest Document Size = 160