【问题标题】:Sorting with arrays as values in MongoDB using java mongo db driver api使用 java mongo db driver api 将数组作为 MongoDB 中的值进行排序
【发布时间】:2015-11-26 06:19:40
【问题描述】:

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

我有两个字段的集合,一个以字符串作为值,第二个字符串数组作为值。

例如

{"name" : "User-1",  "locations" : ["India", "USA", "UK"]}
{"name" : "User-2",  "locations" : ["Russia", "UK", "USA"]}
{"name" : "User-3",  "locations" : ["UK", "Japan", "India"]}
{"name" : "User-4",  "locations" : ["Japan", "USA", "UK"]}
{"name" : "User-5",  "locations" : ["India", "Italy", "Japan"]}

我想对字段“位置”进行排序。

输出必须是

{"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 driver api 做到这一点。

谢谢

【问题讨论】:

  • 如果我们可以使用java,我会覆盖compareTo方法。
  • @Uma。我需要使用java驱动API,API有排序方法。
  • @Sarath。聚合的任何示例代码。
  • @PankajShinde 看到我的答案兄弟 :)
  • @Sarath。我得到了你的答案。我想要java驱动程序api代码。而且您的代码只会对第一个数组元素进行排序。

标签: java arrays mongodb sorting mongodb-query


【解决方案1】:

您可以使用"dot notation" 为数组中的每个项目按顺序指定排序:

 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)
);

【讨论】:

  • 但我们事先并不知道数组元素的总数。那么我们如何为您提到的排序方法的参数编写代码。
  • @如果你真的需要,你可以指定n,但请记住,任何实际上比n短的数组都将始终浮动到顶部。就像对 null 或任何文档的缺失属性进行排序一样。
  • "你可以指定 n",是什么意思。我们需要追加 n 次或使用 'n' 而不是 '0'。
  • @PankajShinde 指定每个排序键,直至达到预期的最大值。最好以这种方式处理它,好像您在处理时尝试计算它会增加很多开销。在构建排序文档之前,您始终可以运行单独的查询以找出最大数组长度。但这与您提出的问题不同。
  • 好的,谢谢。请为追加功能正确的排序顺序代码(你提到 2 而不是 1)。
【解决方案2】:

在 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))));

【讨论】:

  • 谢谢萨拉。您的代码将正常工作。但正如我在问题中所说的,我正在使用 Mongo 的 Java 驱动程序 API。您的答案是查询我们将在 mongo 控制台上执行。谢谢。
  • 很高兴我能帮上忙 :) 在这种情况下,您能否接受它作为正确答案
猜你喜欢
  • 2018-02-21
  • 1970-01-01
  • 2020-01-28
  • 1970-01-01
  • 2018-08-05
  • 2013-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多