Database MongoDB:$lookup之后的组不';行不通
我正在为教育目的创建一个MongoDB数据库。此文档由两个集合组成:字符和对象。这里有一个例子Database MongoDB:$lookup之后的组不';行不通,database,mongodb,collections,nosql,lookup,Database,Mongodb,Collections,Nosql,Lookup,我正在为教育目的创建一个MongoDB数据库。此文档由两个集合组成:字符和对象。这里有一个例子 /* CHARACTERS */ { "_id":"character_name1", /*other no-relevant informations*/ "inventory" : [ { "name":"object_name1"},
/* CHARACTERS */
{
"_id":"character_name1",
/*other no-relevant informations*/
"inventory" : [
{ "name":"object_name1"},
{ "name":"object_name2"}
]
},
{
"_id":"character_name2",
/*other no-relevant informations*/
"inventory" : [
{ "name":"object_name2"},
{ "name":"object_name3"}
]
}
/* OBJECT */
{
"name":"object_name1",
/*other no-important informations*/
"effects" : [
{"health":10},
{"mana":200}
]
},
{
"name":"object_name2",
/*other no-important informations*/
"effects" : [
{"charisma":10},
{"strenght":200}
]
}
现在,我试图显示拥有对象的角色ID,这些对象在“效果”数组中具有关键的“魅力”。
下面这段代码的结果是正确的,但它会打印每个遵守$match语句的对象的所有者ID
db.character.aggregate([
{
$lookup: {
"localField": "inventory.name",
"from": "objects",
"foreignField": "name",
"as": "object_list"
}
},
{
$unwind : "$object_list",
},
{
$match : {
"object_list.effects.charisma": {$exists:true}
}
},
{
$project:{
"_id":"$_id",
"object_name":"$lista_oggetti.nome"
}
}
]
).pretty()
如果我使用$group而不是$project,程序将启动“无限”计算。
有什么建议吗?在$lookup之前,您应该展开库存在$lookup之前,您应该展开库存