在MongoDB的外壳,如果我做了以下内容,然后创建了索引,并且还可以防止插入重复记录:为什么MongoDB可以创建唯一的索引,但Mongoid不能?
db.analytics.ensureIndex({page: 1, some_id: 1, ga_date: -1}, {unique: true});
但我认为Mongoid可以这样做: http://mongoid.org/docs/indexing/
所以,我有:
class PageAnalytic < Analytic
include Mongoid::Document
field :page, :type => String
field :some_id, :type => Integer
field :ga_date, :type => Time
field :pageviews, :type => Integer
field :timeOnPage, :type => Integer
index(
[
[ :page, Mongo::ASCENDING ],
[ :some_id, Mongo::ASCENDING ],
[ :ga_date, Mongo::DESCENDING ]
],
:unique => true
)
end
,并做了
rake db:create_indexes
但仍然可以插入重复记录?
更新:这是很奇怪的,但之后,我加入了MongoDB的壳索引和删除的集合,然后重新无论是在MongoDB的Shell或Mongoid指数,现在我可以删除MongoDB中壳收集,然后rake创建索引,并使用mongoid两次添加相同的文档,并且mongod会为重复键声明错误。
你使用的是什么版本的mongoid? – 2011-04-26 14:15:03