我正在开发纸牌游戏。我的游戏中有一张纸牌目录,比如说“目录”表:
class Sample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
Column(
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height / 2,
width: double.infinity,
decoration: BoxDecoration(
color: Color.fromRGBO(50, 50, 205, 0.3),
image: DecorationImage(
image: NetworkImage(
'https://cdn.pixabay.com/photo/2015/04/23/21/36/auto-736794__340.jpg',
),
fit: BoxFit.cover,
),
),
),
],
),
Scaffold(
backgroundColor: Colors.transparent,
drawer: Drawer(),
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0.0,
title: new Text("Demo"),
centerTitle: true,
),
body: Column(
children: <Widget>[
Expanded(
child: Center(
child: new Container(
decoration: BoxDecoration(
color: Colors.red
),
child: new Text("book"),
),
),
)
],
),
),
],
);
}
}
在另一个桌子上(名称为playerCards),我拥有一个玩家拥有的所有卡牌。在这种结构上,我不想重复我为目录卡设置的所有信息。我只需要存储卡的ID和金额:
[
{
"id": 1,
"name": "my spell",
"type": "spell",
"cost": 120
}
]
我想知道如何查询以以下格式退回玩家的所有卡牌:
[
{
"id": 1,
"card_count_in_book": 10,
"card_count_in_deck": 1
}
]
答案 0 :(得分:2)
使用$lookup运算符通过id
合并两个集合中的数据
db.Catalog.aggregate([
{
$lookup: {
from: "playerCards",
localField: "id",
foreignField: "id",
as: "playercards"
}
},
{
$unwind: "$playercards"
},
{
$project: {
id: 1,
name: 1,
type: 1,
cost: 1,
card_count_in_book: "$playercards.card_count_in_book",
card_count_in_deck: "$playercards.card_count_in_deck",
}
}
])