MongoDB $unwind operatörü, bir belgedeki bir dizi alanını çözümlemek ve dizideki her öğe için ayrı çıktı belgeleri (document) oluşturmak için kullanılır.
Girdi belgesi ile çıktı belgeleri arasındaki tek fark, çıktı belgelerinde dizi alanının değerinin, girdi belgesi dizisinden tek bir öğe ile değiştirilmesidir.
$unwind operatörünün kullanımı:
{ $unwind : "$<alan yolu / dizi yolu>" }
{ $unwind : { path: "$<alan yolu / dizi yolu>" , <isteğe bağlı bağımsız değişkenler>}}
Sorgu (normal)
db.posts.find().pretty()
{
_id : ObjectId("4e6e4ef557b77501a49233f6")
title : "this is my title" ,
author : "bob" ,
tags : [ "fun" , "good" , "nice" ]
}
Sorgu ($unwind)
db.posts.aggregate([{$unwind : "$tags" }]).pretty()
{
"_id" : ObjectId("4e6e4ef557b77501a49233f6"),
"title" : "this is my title",
"author" : "bob",
"tags" : "fun"
},
{
"_id" : ObjectId("4e6e4ef557b77501a49233f6"),
"title" : "this is my title",
"author" : "bob",
"tags" : "good"
},
{
"_id" : ObjectId("4e6e4ef557b77501a49233f6"),
"title" : "this is my title",
"author" : "bob",
"tags" : "nice"
}