gpt4 book ai didi

ruby-on-rails - 如何使嵌入的范围只获取第一个元素?

转载 作者:可可西里 更新时间:2023-11-01 10:44:46 24 4
gpt4 key购买 nike

我有很多坐标嵌入到位。如何只获得每个自放置对象的第一个“开始”坐标?范围是正确的想法吗?我只能选择第一个、最后一个或所有具有所有(非常慢)坐标的位置,我注释掉的范围不起作用。

代码:

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" ) }

最佳答案

范围通常用于根据某些条件过滤您想要的整个 对象的结果集。相反,看起来您正在尝试选择给定文档的某些属性。为此,您应该使用 only 方法。

这可能有效,但不确定确切的语法:

Place.only(":coordinates.0")

关于ruby-on-rails - 如何使嵌入的范围只获取第一个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8263261/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com