Reputation: 1
I have the following input :
{"_id": "series/cogtech/BertonKHS06","type": "Article", "title": "Speech Recognition.", "pages": {"start": 85, "end": 107}, "year": 2006, "booktitle": "SmartKom", "url": "db/series/cogtech/54023732.html#BertonKHS06", "authors": ["Andr? Berton","Alfred Kaltenmeier","Udo Haiber","Olaf Schreiner"]}
I want to get all the authors separetly with their books, I tried this query :
db.publis.find({}, {authors:1, title:2, _id:0})
My expected output :
Book Author
Speech Recognition Andr? Berton
Speech Recognition Alfred Kaltenmeier
Speech Recognition Udo Haiber
Speech Recognition Olaf Schreiner
Upvotes: 1
Views: 186
Reputation: 57095
Demo - https://mongoplayground.net/p/86ZQkXfN4BW
Use $unwind
Deconstructs an array field from the input documents to output a document for each element. Each output document is the input document with the value of the array field replaced by the element.
db.collection.aggregate([
{ $unwind: "$authors" }, // break into individual documents
{ $project: { _id: 0, book: "$title", author: "$authors" } }
])
Upvotes: 1