在 ROR 和 mongoid 作为映射器中,我在模型中遇到了这个类:
class StockPrice
include Mongoid::Document
include Mongoid::Timestamps
field :stock_id, type: String
field :price, type: Float
index :stock_id => +1, :updated_at => -1
def self.price(stock_id)
where(stock_id: stock_id).desc(:updated_at).first.price
end
def self.crawl(stock_id)
# I am stock price from internet using a web crawler
end
end
作为 mongodb 的新手,我有以下疑惑:
1)索引主要用于什么?
2)这一行在代码中传达了什么:
where(stock_id: stock_id).desc(:updated_at).first.price