标签: mongoid

如何在 MongoDB 或 Mongoid 中索引哈希字段?

我有以下蒙戈文档:

{
  _id: 'someid',
  name: 'John Doe',
  address: {
    city: 'Osaka',
    country: 'Japan'
  }
}
Run Code Online (Sandbox Code Playgroud)

如何按城市和国家建立索引?

ruby-on-rails mongodb mongoid

3
推荐指数
1
解决办法
1223
查看次数

在 mongoid 中使用嵌入文档与数组相比有何优缺点?

当您可以简单地使用数组数据类型时,使用嵌入文档的优点和缺点是什么?两者对我来说都很相似(我无法通过谷歌搜索在线找到任何信息)。请提供示例案例!

mongodb mongoid

3
推荐指数
1
解决办法
1604
查看次数

在 Mongoid 3.0 中在运行时切换数据库

我有一个在 Unicorn 上运行的 Sinatra 应用程序,它使用 Mongoid 作为其模型。我有几个结构相同但内容不同的 Mongo 数据库,我在每个用户登录时为每个用户选择正确的数据库。我想知道 Mongoid 3.0 是否可以实现。

mongodb mongoid

3
推荐指数
1
解决办法
1013
查看次数

Embedded_in 在 mongoid 中的意义

我试图了解 mongoid 中的关系,但无法超越以下内容:

class Band
  include Mongoid::Document
  embeds_many :photos
end

class Photo
  include Mongoid::Document
  embedded_in :Band
end
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,Instructing Band to embed_many phtos 将完整的照片数据存储在Band 中。但是,什么需要在 Photo 类中放入 Embedded_in 呢?如果我们不把embedded_in放在Photo里面,会不会自动多态?

此外,查询会Photo.all获取嵌入在 Bands 中的照片吗?如果是,这是embedded_in 的原因吗?我们可以为一个班级设置多个 embedding_in 吗?

polymorphism mongodb mongoid

3
推荐指数
1
解决办法
1125
查看次数

如何使用 Mongoid 重新索引集合?

Mongoid 提供了一些 rake 任务,其中之一为数据库中的所有集合创建索引:

耙 db:create_indexes

但是如果我错了,请纠正我,创建索引与实际索引所有项目是否不同?如何重新索引我的文档?如果我在 Rails 中为新集合添加了索引但数据库中已经有 10,000 个项目,这将是必要的。

mongoid

3
推荐指数
1
解决办法
1809
查看次数

mongoid - 覆盖现有的字段警告

我刚开始使用 mongodb。我有一个通过 mongoid (4.0.0) 使用 mongodb 的 rails 4.0 应用程序有一个 Company 模型和一个 User 模型。所以用户belongs_to :company和公司has_many :users

当我启动控制台时,我会Company.first得到一个我创建的公司。当我第一次Company.first.users

WARN -- : Overwriting existing field 对于所有领域。

然后公司用户按预期。

当我第二次这样做时,没有警告,只有公司用户。

这是为什么?有什么问题吗?我在 Heroku 中有该应用程序,那里也发生了同样的情况。

谢谢。

更新 1

这是模型

class Company
    include Mongoid::Document
    include Mongoid::Timestamps

    field :name
    field :token

    has_many :users
    has_many :feedbacks
    has_many :main_categories
    has_many :departments

end
Run Code Online (Sandbox Code Playgroud)

更新 2

实际上这些警告只发生在 User 模型中

User.all
W, [2013-10-05T21:30:34.522867 #23442]  WARN -- : Overwriting existing field email in class User. …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails mongodb mongoid

3
推荐指数
1
解决办法
2964
查看次数

获取 mongoid 生成的原始 mongo db 查询表达式

我想得到 mongoid 生成的 mongo 查询表达式怎么做?

例如,这是 mongoid 语法

History.where(report_type: params["report_type"]).order_by(ts:1).only(:ts).last
Run Code Online (Sandbox Code Playgroud)

我想拥有诸如 的方法to_sql来获取本机查询表达式,以便我可以在 Mongo 控制台上应用它。

ruby-on-rails mongoid

3
推荐指数
1
解决办法
1251
查看次数

初始化的 Mongoid 重载

我使用带有 rails 模型的 Mongoid 遇到了这个错误:

NoMethodError: 未定义方法 `[]' 为 nil:NilClass

为简化起见,我的类声明如下:

class Fruit
    include Mongoid::Document

    field :name, type: String

    def initialize
        self.name = 'fruit'
    end
end
Run Code Online (Sandbox Code Playgroud)

最初我无法弄清楚这是从哪里来的,所以我开始削减事情。取出 Mongoid::Document 包含解决了问题(但显然并不理想)。在进一步按摩谷歌后,我发现了这个讨论:

https://github.com/mongoid/mongoid/issues/1678

...描述了同样的问题。因为我想使用初始化机制在子类中设置实例变量,所以我想出了这个解决方案:

class Fruit
    include Mongoid::Document

    field :name, type: String, default: ->{ self.do_init }

    def do_init
        self.name = 'fruit'
    end
end
Run Code Online (Sandbox Code Playgroud)

这有效,但似乎不太理想。再说一次,也许没问题。我想发布这个是因为 a) 我很难找到类似问题的描述,并且 b) 我虽然这在 mongoid 中没有很好的记录。

据我了解,Mongoid gem 重载初始化,我尝试覆盖初始化重新重载并破坏了 Mongoid::Document 初始化过程。

ruby ruby-on-rails mongoid

3
推荐指数
1
解决办法
1092
查看次数

Sidekiq 失败,dequeueing mongoid 连接超时,可能连接太多

我目前正在运行sidekiq 4.1.2。我从来没有设法能够同时运行多个作业。最近看起来我遇到了 Sidekiq 的故障排除 WIKI 中描述的问题,称为Too many connections to MongoDB。显然,mongoid 3没有正确断开工人的连接。但是,我正在使用mongoid 5.1.3.

我的问题出现在一个作业,而其他一些作业正在运行时,尝试使用查询访问数据库:

Timeout::Error: Timed out attempting to dequeue connection after 30 sec.
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool/queue.rb:190:in `wait_for_next!'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool/queue.rb:176:in `block in dequeue_connection'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool/queue.rb:190:in `wait_for_next!'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool/queue.rb:176:in `block in dequeue_connection'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool/queue.rb:172:in `loop'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool/queue.rb:172:in `dequeue_connection'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool/queue.rb:62:in `block in dequeue'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool/queue.rb:61:in `synchronize'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool/queue.rb:61:in `dequeue'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool.rb:51:in `checkout'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/connection_pool.rb:107:in `with_connection'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/server/context.rb:63:in `with_connection'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/operation/executable.rb:34:in `execute'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/collection/view/iterable.rb:80:in `send_initial_query'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/collection/view/iterable.rb:41:in `block in each'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/retryable.rb:51:in `call'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/retryable.rb:51:in `read_with_retry'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/collection/view/iterable.rb:39:in `each'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongoid-5.1.3/lib/mongoid/query_cache.rb:207:in `each'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongoid-5.1.3/lib/mongoid/contextual/mongo.rb:121:in `each'
/home/me/applications/myapp/shared/bundle/ruby/2.2.0/gems/mongoid-5.1.3/lib/mongoid/contextual/mongo.rb:295:in …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails mongodb mongoid sidekiq sidetiq

3
推荐指数
1
解决办法
903
查看次数

如何在 mongoid 5 中创建文本索引?

我的模型中有这个

index({company_name: 1, first_name: 1, last_name: 1 })

Model.text_search 'something'

给出这个错误

Mongo::Error::OperationFailure: text index required for $text query

mongodb mongoid mongoid5

3
推荐指数
1
解决办法
668
查看次数