为一本书选择不同的作者 [英] SELECT distinct authors for a book
本文介绍了为一本书选择不同的作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下输入:
{"_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})
我的预期输出:
Book Author
Speech Recognition Andr? Berton
Speech Recognition Alfred Kaltenmeier
Speech Recognition Udo Haiber
Speech Recognition Olaf Schreiner
推荐答案
Demo - https://mongoplayground.net/p/86ZQkXfN4BW
使用 $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" } }
])
这篇关于为一本书选择不同的作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文