使用Objectid的嵌套字段的光标分页
我正在尝试在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 fromitemInfo
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.