MongoDB $size 运算符

MongoDB $size 运算符 介绍

MongoDB 提供了多种数组表达式运算符,$size 运算符就是其中之一。$size 运算符用于查找给定数组中存在的元素总数,它也用于聚合管道阶段。

MongoDB $size 运算符 语法

{ $size: <expression> } 

要点:

  1. 如果参数丢失,那么它会给出错误。
  2. 如果参数不解析为数组,那么它会给出错误。

MongoDB $size 运算符 例子

在以下示例中,我们正在使用:

Database: Yiidian
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 运算符查找“标记”字段的总大小。

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
}

 

热门文章

优秀文章