【问题标题】:MongoMapper returns empty document set on call to `all`MongoMapper 在调用 `all` 时返回空文档集
【发布时间】:2015-10-28 15:56:36
【问题描述】:

我在mongodb有一个收藏:

> use my_db
switched to db my_db
> db.my_coll.count()
278

我有一堂红宝石课:

class MyColl
  include MongoMapper::Document
  key :fst, Integer
  key :sec, Float
end

当我打电话时:

MongoMapper.database = 'my_db'
MyColl.count
#=> 0

虽然我绝对希望收到与 mongo 控制台相同的结果。没有错误,什么都没有。

我在这里错过了什么?

【问题讨论】:

    标签: ruby-on-rails mongodb mongomapper


    【解决方案1】:

    MongoMapperActiveRecord 约定要求类名与集合名对应:

    self.class.name.gsub(/::/, '.').underscore.pluralize == coll_name
    

    也就是说,MyColl 类期望基础集合命名为 my_colls。尽管数据库中没有这样的集合,MongoMapper 默默地假设用户想要透明地创建这个集合。这就是MyColl.count 返回零而不是抛出异常的原因。

    > db.my_coll.renameCollection("my_colls")
    

    会成功的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-17
      • 2016-03-05
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多