简体   繁体   English

对次要字段进行排序和限制的地理空间查询

[英]geo-spatial query with sort and limit on a secondary field

I am trying to obtain the most recently created 3 accounts in the nearby area. 我正在尝试获取附近地区最近创建的3个帐户。 So I tried to use a geo-spatial query combined with a sort(created_at: -1) and limit(3) 因此,我尝试将地理空间查询与sort(created_at:-1)和limit(3)结合使用

The basic geo-spatial query: 基本地理空间查询:

db.users.find({"loc": {$near: [28.41, 77.04], $maxDistance:0.05, $uniqueDocs: true}})
{ "created_at" : ISODate("2013-12-11T07:58:34.927Z"), "name" : "A" }
{ "created_at" : ISODate("2014-03-08T10:00:17.921Z"), "name" : "B" }
{ "created_at" : ISODate("2014-03-13T08:28:46.285Z"), "name" : "C" }
{ "created_at" : ISODate("2014-03-05T12:01:34.199Z"), "name" : "D" }
{ "created_at" : ISODate("2014-03-13T08:16:22.913Z"), "name" : "E" }
{ "created_at" : ISODate("2014-03-13T10:23:02.660Z"), "name" : "F" }

Adding sorting to it gave: 添加排序可以得到:

db.users.find({"loc": {$near: [28.41, 77.04], $maxDistance:0.05, $uniqueDocs: true}}).sort({created_at: -1})
{ "created_at" : ISODate("2014-03-13T10:23:02.660Z"), "name" : "F" }
{ "created_at" : ISODate("2014-03-13T08:28:46.285Z"), "name" : "C" }
{ "created_at" : ISODate("2014-03-13T08:16:22.913Z"), "name" : "E" }
{ "created_at" : ISODate("2014-03-08T10:00:17.921Z"), "name" : "B" }
{ "created_at" : ISODate("2014-03-05T12:01:34.199Z"), "name" : "D" }
{ "created_at" : ISODate("2013-12-11T07:58:34.927Z"), "name" : "A" }

And finally adding a limit gave: 最后加上一个限制:

db.users.find({"loc": {$near: [28.41, 77.04], $maxDistance:0.05, $uniqueDocs: true}}).sort({created_at: -1}).limit(3)
{ "created_at" : ISODate("2014-03-13T08:28:46.285Z"), "name" : "C" }
{ "created_at" : ISODate("2014-03-08T10:00:17.921Z"), "name" : "B" }
{ "created_at" : ISODate("2013-12-11T07:58:34.927Z"), "name" : "A" }

The expected result was [F,C,E] (ie the first 3 entries from the second query-result). 预期结果为[F,C,E](即第二个查询结果的前3个条目)。 But instead I get [C,B,A], which is the first 3 entries from the first query-result (sorted by creation time). 但是我得到的是[C,B,A],它是第一个查询结果的前3个条目(按创建时间排序)。

So mongodb is performing the limit operation before it performs the sort operation. 因此,mongodb在执行排序操作之前先执行限制操作。 Is there any way to force it to sort before applying the limit ? 是否有任何方法可以在应用限制之前强制其排序?

A similar question was asked here , but there was an issue in the query itself. 这里提出类似的问题,但查询本身存在问题。 Is this issue specific to '$near' queries ? 这个问题专门针对“ $ near”查询吗?

The geo-spatial query already sorts the documents from nearest to farthest . geo-spatial查询已将文档从最近到最远进行 排序 So, if you have an additional sort() and limit(), limit() is applied first since the results are already sorted by distance. 因此,如果您还有其他sort()和limit(),则将首先应用limit(),因为结果已经按距离进行了排序。

Although I think it doesn't make much sense to sort by "created_at" in a geo-spatial query, if you still need it, it can only be done programmatically. 尽管我认为在地理空间查询中对“ created_at”进行排序没有多大意义,但是如果您仍然需要它,则只能通过编程来完成。 That is, sort the entire results in the query and apply the limit in your client program. 也就是说,对查询中的整个结果进行排序,然后将限制应用到客户端程序中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM