我在 MongoDB 中有以下数据。仅基于我可用的 id,如何检索该玩家与我当前 id 的玩家匹配的所有其他条目。
例如:查找 id 12 的玩家是谁,搜索与该玩家名称匹配的所有其他条目并返回所有条目的列表。
[
{_id: '62ecdf342f1193134043964c', id: '12', player: 'David Beckham', team: 'Manchester United'},
{_id: '62ecdf342f1193134043965c', id: '17', player: 'Cristiano Rolando', team: 'Manchester United'},
{_id: '62ecdf342f1193134043966c', id: '22', player: 'Cristiano Rolando', team: 'Juventus'},
{_id: '62ecdf342f1193134043967c', id: '42', player: 'David Beckham', team: 'Real Madrid'},
]
这是我用来检索与特定 id 匹配的单个条目的代码,然后我还想获取相关条目。
export async function getStaticProps({ params }) {
const { db } = await connectToDatabase();
const jerseyA = await db
.collection("Jerseys")
.find({ id: params.jersey })
.sort()
.toArray();
const jersey = JSON.parse(JSON.stringify(jerseyA))[0];
return { props: { jersey } };
}
最佳答案
现在您知道了名称,请执行另一次获取,例如 .find({player: jersey.player})
关于reactjs - 从 MongoDB 检索相关元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73261652/