【问题标题】:Indexing coordinates in MongoDB not workingMongoDB中的索引坐标不起作用
【发布时间】:2013-07-11 22:14:42
【问题描述】:

我有一个存储坐标的模型,并且坐标是索引的

class Place
  include Mongoid::Document
  include Mongoid::Spacial::Document

  field :coordinates, type: Array, spacial: true

  index({ coordinates: "2d" }, { unique: true })
end

但是,每当我用一组坐标保存一个地点然后再创建另一个时,两个坐标都会被保存。这使我相信坐标的索引不起作用。我在这里遗漏了什么,我该如何解决?

下面是一个例子。

place = Place.new(coordinates: [50, 50])

place.save # returns true in console

place2 = Place.new(coordinates: [50, 50])

place2.save # returns true in console

# Thus I have two place records with the same exact coordinates, something I don't want

【问题讨论】:

  • 你能举个例子吗?
  • 把它放在我的问题中。

标签: ruby mongodb indexing sinatra mongoid


【解决方案1】:

2d 和 2dsphere 索引类型根本不强制执行 unique 约束。我创建了一个 DOCS 问题以在文档中澄清这一点:https://jira.mongodb.org/browse/DOCS-1701

【讨论】:

  • 很酷,谢谢。我希望目前情况如此。经过进一步研究,它看起来像 Mongoid,我使用的 ODM 需要一个 rake 任务来执行索引,但是,没有解释如何为 Sinatra 应用程序做这样的事情。感谢您向 MongoDB 报告问题的回答和帮助。
猜你喜欢
  • 2012-06-25
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 2015-04-15
  • 2021-10-13
  • 1970-01-01
  • 2020-06-11
  • 2021-12-30
相关资源
最近更新 更多