Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

This is my first day with MongoDB so please go easy with me :)

I can't understand the $unwind operator, maybe because English is not my native language.

db.article.aggregate( 

{ $project : { 

author : 1 , 

title : 1 , 

tags : 1 

}}, 

{ $unwind : "$tags" } 

);

The project operator is something I can understand, I suppose (it's like SELECT, isn't it?). But then, $unwind (citing) returns one document for every member of the unwound array within every source document.

Is this like a JOIN? If yes, how the result of $project (with _id, author, title and tags fields) can be compared with the tags array?

1 Answer

0 votes
by (106k points)

The $unwind operator allows you to peel off a document for each element and returns that resulting document. To think of this in a classical approach, it would be the equivalent of "for each item in the tags array, return a document with only that item".

You can understand it in a much better way by referring the below-mentioned code:-

title : "this is my title" , 

author : "bob" , 

posted : new Date () , 

pageViews : 5 , 

tags : [ "fun" , "good" , "fun" ] , 

comments : [ 

{ author :"joe" , text : "this is cool" } , 

{ author :"sam" , text : "this is bad" } 

],

 other : { foo : 5 } 

}

Related questions

0 votes
2 answers
0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
asked Jan 5, 2020 in Web Technology by ashely (50.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...