与skip()和limit()一起使用的Distinct()命令 [英] Distinct() command used with skip() and limit()

查看:263
本文介绍了与skip()和limit()一起使用的Distinct()命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MongoDB集合中有这些项目:

I have those items in my MongoDB collection:

{x: 1, y: 60, z:100}
{x: 1, y: 60, z:100}
{x: 1, y: 60, z:100}
{x: 2, y: 60, z:100}
{x: 2, y: 60, z:100}
{x: 3, y: 60, z:100}
{x: 4, y: 60, z:100}
{x: 4, y: 60, z:100}
{x: 5, y: 60, z:100}
{x: 6, y: 60, z:100}
{x: 6, y: 60, z:100}
{x: 6, y: 60, z:100}
{x: 7, y: 60, z:100}
{x: 7, y: 60, z:100}

我想查询x的不同值(即 [1、2、3、4、5、6、7] )...但是我只想要它们的一部分(类似到我们可以通过skip(a)和limit(b)获得的结果.

I want to query the distinct values of x (i.e. [1, 2, 3, 4, 5, 6, 7]) ... but I only want a part of them (similar to what we can obtain with skip(a) and limit(b)).

我该如何使用MongoDB的Java驱动程序(如果可能的话,使用spring-data-mongodb)呢?

How do I do that with the java driver of MongoDB (or with spring-data-mongodb if possible) ?

推荐答案

在mongo shell中使用聚合框架很简单:

in mongo shell is simple with aggregate framework:

db.collection.aggregate([{$group:{_id:'$x'}}, {$skip:3}, {$limit:5}])

对于Java外观:在Java中使用聚合框架

这篇关于与skip()和limit()一起使用的Distinct()命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆