Mongo-获取中介文件的数量
目前,我有一个查询:
const result = await getInstances();
为我提供了一系列文档:
[{name: "first", age: 13},
{name: "second", age: 21},
{name: "third", age: 11},
{name: "fourth", age: 14}
...]
查询是这样的:
...
return Instances.aggregate
.match({//condition})
.skip(skipValue).limit(pageSize) // pagination done here
我想要一个查询,以附加总数。分页之前的文档,但返回分页的数据,例如:
...
return Instances.aggregate
.match({//condition}) ## I WANT THE COUNT OF THIS STEP TO BE APPENDED
.<SOME_PIPELINE_HERE>
.skip(skipValue).limit(pageSize) // pagination done here
将返回类似的内容:
{
data: [{name: "first", age: 12}....<ALL_PAGINATED_DATA>],
totalCount: 54 #count of data before pagination
}
我尝试过的和不起作用的内容:
Instances.aggregate()
.match({//CONDITION})
.addFields({count: {$size: _id}})
.skip(value).limit(value)
看来,它可以通过并为每个文档而不是整个文档进行计算
currently I have a query:
const result = await getInstances();
that provides me an array of document:
[{name: "first", age: 13},
{name: "second", age: 21},
{name: "third", age: 11},
{name: "fourth", age: 14}
...]
The query goes something like this:
...
return Instances.aggregate
.match({//condition})
.skip(skipValue).limit(pageSize) // pagination done here
I want a query that appends a count for the total no. of documents before the pagination, but returns the paginated data, e.g:
...
return Instances.aggregate
.match({//condition}) ## I WANT THE COUNT OF THIS STEP TO BE APPENDED
.<SOME_PIPELINE_HERE>
.skip(skipValue).limit(pageSize) // pagination done here
would return something like:
{
data: [{name: "first", age: 12}....<ALL_PAGINATED_DATA>],
totalCount: 54 #count of data before pagination
}
What I tried and didn't work:
Instances.aggregate()
.match({//CONDITION})
.addFields({count: {$size: _id}})
.skip(value).limit(value)
It seems it goes through and calculates this for each document instead of the whole
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种选项是使用
$ facet
以“分叉”中间的查询,因此可以在不同的管道上使用相同的数据。例如:请参阅游乐场示例
One option is to use
$facet
in order to "fork" the query in the middle, so the same data can be used on different pipelines. For example:See how it works on the playground example