Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java mongo db驱动程序api在MongoDB中将数组作为值进行排序_Java_Arrays_Mongodb_Sorting_Mongodb Query - Fatal编程技术网

使用java mongo db驱动程序api在MongoDB中将数组作为值进行排序

使用java mongo db驱动程序api在MongoDB中将数组作为值进行排序,java,arrays,mongodb,sorting,mongodb-query,Java,Arrays,Mongodb,Sorting,Mongodb Query,在MongoDB中将数组作为值进行排序 我的集合有两个字段,一个是字符串作为值,另一个是字符串数组作为值 e、 g 我想按字段“位置”排序 输出必须是 {"name" : "User-5", "locations" : ["India", "Italy", "Japan"]} {"name" : "User-1", "locations" : ["India", "USA", "UK"]} {"name" : "User-4", "locations" : ["Japan", "USA",

在MongoDB中将数组作为值进行排序

我的集合有两个字段,一个是字符串作为值,另一个是字符串数组作为值

e、 g

我想按字段“位置”排序

输出必须是

{"name" : "User-5",  "locations" : ["India", "Italy", "Japan"]}
{"name" : "User-1",  "locations" : ["India", "USA", "UK"]}
{"name" : "User-4",  "locations" : ["Japan", "USA", "UK"]}
{"name" : "User-2",  "locations" : ["Russia", "UK", "USA"]}
{"name" : "User-3",  "locations" : ["UK", "Japan", "India"]}
我们如何使用mongo db java驱动程序api实现这一点


谢谢

在mongo控制台中尝试此功能。这使用mongodb聚合框架:

db.users.aggregate({$sort:{'locations':1}})
这对我来说真是出人意料。我使用各种测试样本进行了测试。如果有的话,请检查角落的箱子。以下是测试用例之一:

> db.users.aggregate({$sort:{'locations':1}})
{ "_id" : ObjectId("5656ce39cb0a925b3d5d16ea"), "name" : "User-6", "locations" : [ "India", "Italy", "Alabama" ] }
{ "_id" : ObjectId("5656ce44cb0a925b3d5d16eb"), "name" : "User-6", "locations" : [ "India", "Italy", "Glabama" ] }
{ "_id" : ObjectId("5656ce75cb0a925b3d5d16ed"), "name" : "User-8", "locations" : [ "India", "Italy", "Glabama", "Cloverfield" ] }
{ "_id" : ObjectId("5656ce63cb0a925b3d5d16ec"), "name" : "User-7", "locations" : [ "India", "Italy", "Glabama", "Govindam" ] }
{ "_id" : ObjectId("5656a65ecb0a925b3d5d16e9"), "name" : "User-5", "locations" : [ "India", "Italy", "Japan" ] }
{ "_id" : ObjectId("5656a65ecb0a925b3d5d16e5"), "name" : "User-1", "locations" : [ "India", "USA", "UK" ] }
{ "_id" : ObjectId("5656a65ecb0a925b3d5d16e8"), "name" : "User-4", "locations" : [ "Japan", "USA", "UK" ] }
{ "_id" : ObjectId("5656a65ecb0a925b3d5d16e6"), "name" : "User-2", "locations" : [ "Russia", "UK", "USA" ] }
{ "_id" : ObjectId("5656a65ecb0a925b3d5d16e7"), "name" : "User-3", "locations" : [ "UK", "Japan", "India" ] }
在Java API中,应该是这样的:

AggregateIterable<Document> iterable = db.getCollection("users").aggregate(asList(
  new Document("$sort", new Document("locations", 1))));
AggregateIterable=db.getCollection(“用户”).aggregate(asList(
新文件(“$sort”,新文件(“locations”,1));

您可以按顺序为数组中的每个项目指定排序方式:

 db.docs.find().sort({ "locations.0": 1, "locations.1": 1, "locations.2": 1 })
这将产生:

{ "name" : "User-5", "locations" : [ "India", "Italy", "Japan" ] }
{ "name" : "User-1", "locations" : [ "India", "USA", "UK" ] }
{ "name" : "User-4", "locations" : [ "Japan", "USA", "UK" ] }
{ "name" : "User-2", "locations" : [ "Russia", "UK", "USA" ] }
{ "name" : "User-3", "locations" : [ "UK", "Japan", "India" ] }
所以它基本上考虑了数组中每个元素的优先顺序

对于基本Java API,您只需提供相同的排序参数:

collection.find().sort(
    new Document("locations.0",1)
        .append("locations.1",1)
        .append("locations.2",1)
);

如果我们可以使用java,我会重写
compareTo
方法。@Uma。我需要使用java驱动程序API,API有排序方法。@Sarath。任何聚合的示例代码。@PankajShinde查看我的答案bro:)@Sarath。我得到了你的答案。我想要java驱动程序api代码。您的代码将只对第一个数组元素进行排序。但我们事先不知道数组元素的总数。那么,我们如何为您提到的排序方法的参数编写代码。@如果确实需要,您可以指定为
n
,但请记住,任何实际短于
n
的数组都将始终浮到顶部。就像对
null
或任何文档的缺失属性进行排序一样,“您可以指定为n”,意味着什么。我们需要追加n次,或者使用“n”而不是“0”。@PankajShinde将每个排序键指定为预期的最大值。最好是这样处理,因为如果在处理过程中尝试计算,会增加很多开销。在构造排序文档之前,始终可以运行单独的查询以找出最大数组长度。但这和你问的问题不同。好的,谢谢。请更正追加函数的排序顺序代码(您提到的是2而不是1)。谢谢Sarath。你的代码可以正常工作。但正如我在问题中所说的,我正在使用Mongo的Java驱动程序API。您的答案是我们将在mongo控制台上执行的查询。谢谢。很高兴我能帮上忙:)那样的话,你能接受它作为正确答案吗
collection.find().sort(
    new Document("locations.0",1)
        .append("locations.1",1)
        .append("locations.2",1)
);