查找MongoDB中最大的文档大小?
要在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
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练