MongoDB $unwind 运算符

MongoDB $unwind 运算符 介绍

MongoDB 提供了多种状态运算符。$unwind 运算符就是这些运算符之一。$unwind 运算符用于解构文档中的数组字段,并为数组中的每个项目创建单独的输出文档。

输入文档和输出文档之间的唯一区别是输出文档中的数组字段的值被替换为输入文档数组中的单个项目。$unwind 运算符将复杂的文档分解成更小的部分,使它们更易于阅读和理解。

MongoDB $unwind 运算符 语法

{ $unwind: <field path> }  

要点:

  1. 如果 field-path 选项的值不是数组,则会产生错误。
  2. 如果输入文档中不存在字段路径,则不显示输出。
  3. 如果数组为空,则不显示输出。

让我们举个例子来更好地理解 $unwind 运算符的概念。

MongoDB $unwind 运算符 例子

示例 1:在数组上使用 $unwind 运算符

创建员工集合。

db.employee.insertOne(  
    {  
        "name" : "Mikky",  
        "age" : 31,  
        "phone_no" : 8654793212  
        "company" : "javatpoint",  
        "skills" : ["C", "C++", "PHP", "Java", ".Net", ]  
    }  
);  

现在,使用 find() 方法显示员工集合中的文档。

db.employee.find({}).pretty()  

输出结果为:

{
    "_id" : ObjectId("456187864hfh5421h510"),
    "name" : "Mikky",
    "age" : 31,
    "phone_no" : 8654793212
    "company" : "yiidian",
    "skills" : [ 
        "C", 
        "C++",
        "PHP", 
        "Java", 
        ".Net"
    ]
}

如您所见,“skills”字段是一个包含 5 个项目(“C”、“C++”、“PHP”、“Java”、“.Net”)的数组。

现在,使用 $unwind 运算符并查看输出的样子。

db.users.aggregate({  
    $project : {  
        name : 1,  
        phone_no : 1,  
        age : 1,  
        skills : 1  
    }},  
    { $unwind: "$skills" }  
)  

输出结果为:

/* 1 */
{
    "_id" : ObjectId("456187864hfh5421h510"),
    "name" : "Mikky",
     "phone_no" : 8654793212,
    "age" : 31,
    "skills" : "C"
}

/* 2 */
{
    "_id" : ObjectId("456187864hfh5421h510"),
    "name" : "Mikky",
     "phone_no" : 8654793212,
    "age" : 31,
    "skills" : "C++"
}

/* 3 */
{
    "_id" : ObjectId("456187864hfh5421h510"),
    "name" : "Mikky",
     "phone_no" : 8654793212,
    "age" : 31,
    "skills" : "PHP"
}

/* 4 */
{
    "_id" : ObjectId("456187864hfh5421h510"),
    "name" : "Mikky",
     "phone_no" : 8654793212,
    "age" : 31,
    "skills" : "Java"
}

/* 5 */
{
    "_id" : ObjectId("456187864hfh5421h510"),
    "name" : "Mikky",
     "phone_no" : 8654793212,
    "age" : 31,
    "skills" : ".Net"
}

正如您在输出中看到的那样,我们在不同的元素中拥有所有五种技能。

示例 2:在嵌入式数组上使用 $unwind 运算符

在嵌入式数组上使用 $unwind 运算符时,它的作用与常规数组相同。

现在,使用以下文档创建产品集合。

db.product.insertMany([  
  {  
    _id: "1",  
    "items" : [  
       {  
         "name" : "copy",  
         "work" : [ "write", "office" ],  
         "cost" : 10,  
         "total_quantity" : 5  
       },  
       {  
         "name" : "pencil",  
         "work" : [ "write", "school" ],  
         "cost" : 2,  
         "total_quantity" : 5  
       }  
    ]  
  },  
  {  
    _id: "2",  
    "items" : [  
       {  
         "name" : "monitor",  
         "work" : [ "collage", "office" ],  
         "cost" : 5000,  
         "total_quantity" : 1  
       },  
       {  
         "name" : "mouse",  
         "work" : [ "laptop", "CPU" ],  
         "cost" : 300,  
         "total_quantity" : 5  
     }  
    ]  
  }  
])  

现在,对上述文档中的“items”嵌入数组执行 $unwind 运算符。

db.product.aggregate({$unwind: "$items"}).pretty()  

输出结果为:
 

{ 
     "_id" : "1", 
     "items" : { 
                 "name" : "copy", 
                 "work" : [ "write", "office" ], 
                 "cost" : 10, 
                 "total_quantity" : 5 
               } 
}
{  a
     "_id" : "1", 
     "items" : { 
                 "name" : "pencil", 
                 "work" : [ "write", "school" ], 
                 "cost" : 2, 
                 "total_quantity" : 5 
               } 
}
{     
      "_id" : "2", 
      "items" : { 
                  "name" : "monitor", 
                  "work" : [ "collage", "office" ], 
                  "cost" : 5000, 
                  "total_quantity" : 1 
                } 
}
{ 
      "_id" : "2", 
      "items" : { 
                  "name" : "mouse", 
                  "work" : [ "laptop", "CPU" ], 
                  "cost" : 300, 
                  "total_quantity" : 5 
                } 
}

正如您在输出中看到的那样,我们将嵌入式数组的所有四个项目都分开了。您可以使用“work”数组进一步分解它。

db.product.aggregate([{ $unwind : "$items" },{ $unwind : "$items.work" }]).pretty()  

输出结果为:

{ "_id" : "1", "items" : { "name" : "copy", "work" : "write", "cost" : 10, "total_quantity" : 5 } }
{ "_id" : "1", "items" : { "name" : "copy", "work" : "office", "cost" : 10, "total_quantity" : 5 } }
{ "_id" : "1", "items" : { "name" : "pencil", "work" : "write", "cost" : 2, "total_quantity" : 5 } }
{ "_id" : "1", "items" : { "name" : "pencil", "work" : "school", "cost" : 2, "total_quantity" : 5 } }
{ "_id" : "2", "items" : { "name" : "monitor", "work" : "collage", "cost" : 5000, "total_quantity" : 1 } }
{ "_id" : "2", "items" : { "name" : "monitor", "work" : "office", "cost" : 5000, "total_quantity" : 1 } }
{ "_id" : "2", "items" : { "name" : "mouse", "work" : "laptop", "cost" : 300, "total_quantity" : 5 } }
{ "_id" : "2", "items" : { "name" : "mouse", "work" : "CPU", "cost" : 300, "total_quantity" : 5 } }

 

热门文章

优秀文章