MongoDB $size 运算符

MongoDB $size 运算符

MongoDB 中的 $size 运算符是什么

MongoDB 提供了一系列的数组表达式运算符,size 运算符是其中之一。size 运算符用于查找给定数组中元素的总数,并且它也可用于聚合管道阶段。

$size 运算符的语法

{ $size:  }

重要点

  1. 如果缺少参数,则会出错。
  2. 如果参数不是数组,也会出错。

示例

在下面的示例中,我们正在处理:

Database: JavaTpoint
Collection: students
Document: Six documents that contain the details of the students
>db.students.find().pretty()
{
   "_id" : 1,
   "name" : "Jonny",
   "class" : "X",
   "rollNo" : 401,
   "age" : 18,
   "marks" : [ 55, 60, 70, 45, 95, 68 ],
   "extraMarks" : {
                    "practical" : [ 21, 18, 25, 30 ],
                    "attendance" : [ 5, 9 ]
                  }
   "gender" : "Male",
   "bloodgroup" : "A+"
}
{
   "_id" : 2,
   "name" : "Carry",
   "class" : "IX",
   "rollNo" : 35,
   "age" : 17,
   "marks" : [ 85, 40, 90, 75, 85, 77 ],
   "gender" : "Male",
   "bloodgroup" : "B+"
}
{
   "_id" : 3,
   "name" : "Jin",
   "class" : "IX",
   "rollNo" : 49,
   "age" : 17,
   "marks" : [ 85, 70, 80, 95, 94, 81 ],
   "gender" : "Female",
   "bloodgroup" : "O+"
}
{
   "_id" : 4,
   "name" : "Thomas",
   "class" : "X",
   "rollNo" : 61,
   "age" : 18,
   "marks" : [ 91, 65, 71, 63, 98, 76 ],
   "extraMarks" : {
                    "practical" : [ 26, 28, 25, 29 ],
                    "attendance" : [ 8, 8 ]
                  }
   "gender" : "Male",
   "bloodgroup" : "A+"
}
{
   "_id" : 5,
   "name" : "Mia",
   "class" : "IX",
   "rollNo" : 308,
   "age" : 17,
   "marks" : [ 97, 98, 95, 98 ],
   "gender" : "Female",
   "bloodgroup" : "B+"
}
{
   "_id" : 6,
   "name" : "Oats",
   "class" : "IX",
   "rollNo" : 75,
   "age" : 18,
   "marks" : [ 99, 98, 98, 95, 96 ],
   "gender" : "Male",
   "bloodgroup" : "A+"
}

示例 1: 使用$size操作符

在这个示例中,我们将使用$size操作符来查找”marks”字段的总大小。

db.students.aggregate(
   [
     { match : { class : "IX" }},
     {project:
          {
            _id : 0,
            name : 1,
            class : 1,
            rollNo : 1,
            marks : 1,
            gender : 1,
            markssize : { size: "marks" }
          }
     }
   ]
)

输出:

{
   "name" : "Carry",
   "class" : "IX",
   "rollNo" : 35,
   "marks" : [ 85, 40, 90, 75, 85, 77 ],
   "gender" : "Male",
   "markssize" : 6
}
{
   "name" : "Jin",
   "class" : "IX",
   "rollNo" : 49,
   "marks" : [ 85, 70, 80, 95, 94, 81 ],
   "gender" : "Female",
   "markssize" : 6
}
{
   "name" : "Mia",
   "class" : "IX",
   "rollNo" : 308,
   "marks" : [ 97, 98, 95, 98 ],
   "gender" : "Female",
   "markssize" : 4
}
{
   "name" : "Oats",
   "class" : "IX",
   "rollNo" : 75,
   "marks" : [ 99, 98, 98, 95, 96 ],
   "gender" : "Male",
   "markssize" : 5
}

示例2:在嵌入文档中使用$size运算符

在这个示例中,我们要使用$size运算符来找出extraMarks.practical字段的总大小。

db.students.aggregate(
   [
     { match : { class : "X" }},
     {project:
          {
            _id : 0,
            name : 1,
            class : 1,
            rollNo : 1,
            marks : 1,
            extraMarks : 1
            gender : 1,
            result : { size: "extraMarks.practical" }
          }
     }
   ]
)

输出:

{
   "name" : "Jonny",
   "class" : "X",
   "rollNo" : 401,
   "marks" : [ 55, 60, 70, 45, 95, 68 ],
   "extraMarks" : {
                    "practical" : [ 21, 18, 25, 30 ],
                    "attendance" : [ 5, 9 ]
                  }
   "gender" : "Male",
   "result" : 4
}
{
   "name" : "Thomas",
   "class" : "X",
   "rollNo" : 61,
   "marks" : [ 91, 65, 71, 63, 98, 76 ],
   "extraMarks" : {
                    "practical" : [ 26, 28, 25, 29 ],
                    "attendance" : [ 8, 8 ]
                  }
   "gender" : "Male",
   "result" : 4
}

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程