Mongoid:如何在很多对很多人中重新排序关联ID

时间:2018-04-13 00:15:47

标签: ruby-on-rails mongodb mongoid

我是mongodb和mongoid的新手,我正试图弄清楚如何在多对多的关系中重新排序_ids数组。

鉴于这两个模型

class Event
  include Mongoid::Document
  include Mongoid::Timestamps

  field :name, type: String

  has_and_belongs_to_many :performers
end

class Performer
  include Mongoid::Document
  include Mongoid::Timestamps

  field :name, type: String

  has_and_belongs_to_many :events
end

数据运行良好,但我很好奇我将如何在事件文档的performer_ids上设置特定的顺序。

有没有快速的方法可以做到这一点,还是只是在订单发生变化时更新完整记录?

更新:这是我看到文档的方式

{
    "_id" : ObjectId("5acfdbb13ae740cf10015d2d"),
    "name" : "Edna O'Brien",
    "updated_at" : ISODate("2018-04-12T22:20:33.105Z"),
    "created_at" : ISODate("2018-04-12T22:20:33.105Z"),
    "performer_ids" : [ 
        ObjectId("5acfda693ae740ce8c016bab"), 
        ObjectId("5acfda773ae740ce8c017959"), 
        ObjectId("5acfda703ae740ce8c017299")
    ]
}

我希望能够控制performer_ids的顺序,例如,如果我以后希望能够重新命令表演者在前端显示。

  {
    "_id" : ObjectId("5acfdbb13ae740cf10015d2d"),
    "name" : "Edna O'Brien",
    "updated_at" : ISODate("2018-04-12T22:20:33.105Z"),
    "created_at" : ISODate("2018-04-12T22:20:33.105Z"),
    "performer_ids" : [ 
        ObjectId("5acfda703ae740ce8c017299"),
        ObjectId("5acfda773ae740ce8c017959"),
        ObjectId("5acfda693ae740ce8c016bab")

    ]
}

0 个答案:

没有答案