此查询的起点是一个名为book的集合:
{
'_id' {"$oid": "64ba6a416504f1f01d773faa"},
'isbn': 8765434567890,
'booktitle': 'some text',
'chapters':
[
{
'r_id': {"$oid": "64ba6a416504f1f01kkk333"},
'pagenr': 7,
'title': 'whatever',
},
{
'r_id': {"$oid": "64ba6a416504f1f01dj3jd7f"},
'pagenr': 12,
'title': 'another title',
},
]
}
当然,还有更多的事情要做.但这个集合是查询的起点.
第二个集合在不同的集合中有关于许多事情的注释,其中包括关于上面集合中的章节的注释:
{
'_id' {"$oid": "64ba6a416504f1f01d773faa"},
'r_id': {"$oid": "64ba6a416504f1f01kkk333"},
'note': 'blabla',
},
{
'_id' {"$oid": "64ba6a416504f1f01d888hhh"},
'r_id': {"$oid": "64ba6a416504f1f01kkk333"},
'note': 'more blabla',
},
{
'_id' {"$oid": "55353536504f1f01d000000"},
'r_id': {"$oid": "64ba6a416swldkjfdskjl34"},
'note': 'blabla',
},
因此,同一章可以有更多的注释.由两个集合体中的r_id个OID相连.
我需要的是两件几乎相似的东西:
1获得一本书的完整记录由isbn nr. 并将amount of notes per chapter添加到该记录中.
2获得一本书的完整记录由isbn nr. 并在该记录上加all of the notes per chapter.
NB章节在集合中不排序,但应按输出中的章节.页面进行排序.
因此,1的输出应该类似于:
{
'_id' {"$oid": "64ba6a416504f1f01d773faa"},
'isbn': 8765434567890,
'booktitle': 'some text',
'chapters':
[
{
'r_id': {"$oid": "64ba6a416504f1f01kkk333"},
'pagenr': 7,
'title': 'whatever',
'notes': 2
},
{
'r_id': {"$oid": "64ba6a416504f1f01dj3jd7f"},
'pagenr': 12,
'title': 'another title',
'notes': 0
},
]
}
对于2个音符,当然会包含音符array.几乎一样,但不是这样.
到目前为止,我想出的是:
match = {'$match': {'isbn': {'$in': isbns}}}
project = {'$project':
{
'_id': '$_id',
'isbn': '$isbn',
'booktitle': '$booktitle',
'chapters': {'$sortArray':
{
'input': {'$map':
{'input':
'$chapters',
'in': {
'pagenr': '$$this.pagenr',
'title': '$$this.title',
'r_id': '$$this.r_id',
}
}
},
'sortBy': {'pagenr': 1},
}
},
'notes': '$$related_notes',
}
}
lookup = {'$lookup':
{
'from': 'notes',
'localField': 'chapters.r_id',
'foreignField': 'r_id',
'as': 'related_notes',
},
}
pipeline = [
lookup,
match,
project,
]
这行得通,注释当然会添加到输出的末尾.如果我把它们放在$map部分,它们会被重复无数次,并且不会以任何方式过滤.
顺便说一句,我来自SQL,我真的很喜欢Mongo.这很奇妙,但并不容易掌握.了解了很多关于它的知识.
谢谢你的帮助.