我已经阅读了 mongoid 文档,但对我正确理解如何正确索引嵌入文档感到不自信。请看一下这个小代码片段,如果我在正确的轨道上,请告诉我!
标准
temp = Array.new
temp.push(BSON::ObjectId("some_bson_objecId"))
temp.push(BSON::ObjectId("some_other_bson_objecId"))
Game.any_of({'players.user_id' => @user.id},{:_id => temp})
游戏模型
embeds_many :players
index(
[
[ "players.user_id" ],
[ "players.user_name" ]
],
unique: true
)
播放器型号
embedded_in :game
field :user_id, :type => BSON::ObjectId
field :user_name, :type => String, :default => ""
index(
[
[ :user_id ],
[ :user_name ]
],
unique: true
)