MongoDB $unwind 运算符

2024 年 9 月 6 日 | 阅读 4 分钟

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

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

语法

要点

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

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

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

创建一个 employee 集合。

现在,使用 find() 方法显示来自 employee 集合的文档。

输出

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

正如您所看到的,"skills" 字段是一个包含 5 个项目("C", "C++", "PHP", "Java", ".Net")的数组。

现在,使用 $unwind 运算符,看看输出是什么样子的。

输出

/* 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 运算符时,它的作用与常规数组相同。

现在,使用以下文档创建一个 product 集合。

现在,在上面的文档中对 "items" 嵌入式数组执行 $unwind 运算符。

输出

{ 
     "_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" 数组进一步分解它。

输出

{ "_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 } }

下一个主题MongoDB $abs 运算符