我有很多坐标嵌入到位。如何只获得每个自我Place对象的第一个“开始”坐标?范围是正确的想法?我只能选择所有(非常慢)坐标的第一个,最后一个或所有地方,我的注释范围不起作用。
代码:
class Place
include Mongoid::Document
field :title, :type => String
embeds_many :coordinates
# def self.start_coordinate
# self.coordinates.first
# first = self.coordinates.first
## first = self.find({}, { "coordinates" => { "_id" => firstobj?}})
## first = self.find({}, { "coordinates" => {}, :limit=>1})
## self.includes(:coordinates).first
## self.collection.(:coordinates).find_one()
### self.all
# end
end
class Coordinate
include Mongoid::Document
include Mongoid::Spacial::Document
field :coordinates, :type => Array, spacial: true
spacial_index :coordinates
embedded_in :place, :inverse_of => :coordinate
end
MongoDB放置对象:
{ "_id" : ObjectId( "4ece5a04ca6a175b08000016" ),
"coordinates" : [
{ "lat" : 51.54983275438141,
"lng" : 17.31981750561522,
"_id" : ObjectId( "4ece5a04ca6a175b08000002" ) },
{ "lat" : 51.55282151156834,
"lng" : 17.35552307202147,
"_id" : ObjectId( "4ece5a04ca6a175b08000003" ) },
{ "lat" : 51.53830285151265,
"lng" : 17.39397522045897,
"_id" : ObjectId( "4ece5a04ca6a175b08000004" ) } ],
"created_at" : Date( 1322146308000 ),
"description" : "description",
"title" : "test",
"updated_at" : Date( 1322154405000 ),
"user_id" : ObjectId( "4ecd7d4eca6a175783000010" ) }
答案 0 :(得分:1)
范围通常用于根据某些条件过滤所需的整个对象的结果集。相反,看起来您正在尝试选择给定文档的某些属性。要做到这一点,你应该使用唯一的方法。
这可能有效,但不确定确切的语法:
Place.only(":coordinates.0")