使用Objectid的嵌套字段的光标分页

发布于 2025-02-03 17:00:53 字数 770 浏览 4 评论 0原文

我正在尝试在MongoDB系列中拨打嵌套的文档。

这是我数据的样子的示例。我添加了uid字段,其值为mongo objectid,希望它可以将我指向item> itemInfo array中的下一个对象。

const item = {
  _id: ObjectID,
  name: 'Test',
  itemInfo : [
    {
      uid: "ObjectID",
      itemData: [{}, {}]
    },
    {
      uid: "ObjectID",
      itemData: [{}, {}]
    },
    {
      uid: "ObjectID",
      itemData: [{}, {}]
    },
    {
      uid: "ObjectID",
      itemData: [{}, {}]
    }
  ]
}

但是,在下面运行此查询将返回我数据库中的下一个root对象,而不是itemInfo array中的下一个对象。

  const pageSkin = await db
    .find({ "itemInfo.uid": { $gt: new ObjectId("UidFromItemInfoArray") } })
    .limit(pgLimit + 1)

我该如何达到所需的解决方案?

I am trying to paginate a nested document in a mongoDB collection.

This is a sample of what my data looks like. I added uid field whose value is a mongo ObjectId with the hope that it's going to point me to the next object in the itemInfo array.

const item = {
  _id: ObjectID,
  name: 'Test',
  itemInfo : [
    {
      uid: "ObjectID",
      itemData: [{}, {}]
    },
    {
      uid: "ObjectID",
      itemData: [{}, {}]
    },
    {
      uid: "ObjectID",
      itemData: [{}, {}]
    },
    {
      uid: "ObjectID",
      itemData: [{}, {}]
    }
  ]
}

However, Running this query below returns the next root objects in my database and not the next object in the itemInfo array.

  const pageSkin = await db
    .find({ "itemInfo.uid": { $gt: new ObjectId("UidFromItemInfoArray") } })
    .limit(pgLimit + 1)

How can i achieve my desired solution ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮凉 2025-02-10 17:00:53

MongoDB是一个面向文档的数据库,意味着它使用文档而不是子图案运行。

您可以将聚合框架用于 $ undind > itemInfo 因此,您将拥有单个文档,其中完全来自itemInfo每个项目的数组。然后,您可以在下一阶段应用逻辑。

即使可以,也不应该。文档尺寸仅限于16MB,并且数组中的项目顺序是一致的。使用简单的数组操作员在应用级别上获取整个文档和分页量的分类将更加有效,例如 slice

Mongodb is a document oriented database, means it operates with documents, not subdocuments.

You can use an aggregation framework to $unwind itemInfo so you will have individual documents with exactly 1 item from itemInfo array for each item. Then you can apply your logic on the next stage.

Even though you can, you shouldn't. The document size is limited to 16Mb, and order of items in the array is consistent. It will be more efficient to fetch whole document and paginate subdocuments on application level with simple array operators, e.g. slice in nodejs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文