MongoDB提取子文档作为实际的文档
我有一个文件结构,如:
{ _id: '323423423fsdr2', title: 'hey', location: 'Someplace', draft: { title: 'hey im a draft', location: 'Someplace', } }
我如何查询mongo来获取草稿版本,但是如果它是根级别文档,则返回它:
db.collection.find(somequery)
回报
{ title: 'hey im a draft', location: 'Someplace' }
我正在研究汇总框架,但我不确定如何开始。 如果我可以避免在应用程序级别提取文档,这将是很酷的。
聚合是在这种情况下唯一的select,使用$match
来传递你的filter在这里看到更多
db.collection.aggregate([{ $project : { _id : 0, title : "$draft.title", location : "$draft.location" } } ])