MongoDB $unwind操作符
MongoDB提供了各种状态操作符。$unwind
操作符就是其中之一。$unwind
操作符用于解构文档中的数组字段,并为数组中的每个项创建单独的输出文档。
输入文档和输出文档之间唯一的区别是输出文档中的数组字段的值被替换为输入文档数组中的单个项。$unwind
操作符将复杂的文档分解为更小的片段,使它们更易于阅读和理解。
语法
{ $unwind: }
重要要点:
- 如果field-path选项的值不是数组,则会生成错误。
- 如果输入文档中不存在字段路径,则不会显示输出。
- 如果数组为空,则不会显示输出。
让我们通过一个示例更好地理解$unwind运算符的概念。
示例1:在数组上使用$unwind运算符
创建一个员工集合。
db.employee.insertOne(
{
"name" : "Mikky",
"age" : 31,
"phone_no" : 8654793212
"company" : "javatpoint",
"skills" : ["C", "C++", "PHP", "Java", ".Net", ]
}
);
现在,使用find()方法显示employee集合中的文档。
db.employee.find({}).pretty()
输出:
{
"_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操作符并查看输出的样式如何。
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
}
}
如你所见,在输出中,我们有嵌套数组的四个项目是分开的。你可以进一步通过“工作”数组进行拆分。
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 } }