reactjs - 从 MongoDB 检索相关元素

标签 reactjs mongodb next.js mongodb-query

我在 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/

相关文章:

css - 如何使下拉内容在溢出时向右流动

javascript - 以非阻塞调用 JavaScript 的方式调用 API

linux - 更改 Mongodb 的 dbPath

mongodb - Ember、Ember 数据和 MongoDB 的 _id

javascript - NextJs 路由器推送通过 urlObject 传递的未知 key

reactjs - 在 Next Js 中使用 Firebase 云消息推送通知

reactjs - 需要在窗口大小调整时更新 React 组件

reactjs - React 缓存我的 API 请求

javascript - 组件中的 store.getState 或 mapStateToProps

java - 如何知道用于创建 .jar 文件的 mongodb 版本?