我有一个要求,我需要运行如下所示的MongoDB查询:
db.collection.find({ $or : [{"field1" : "value1"}, {"field2" : "value2"}],
$or : [{"field3" : "value3"}, {"field4" : "value4"}]})
即
(field1 == value 1 or field2 == value2) and (field3 == value3 or field4 == value4)
我想通过标准链来实现这一点,因为查询是从代码的不同部分动态形成的.但是,如果我尝试做类似以下的事情
criteria = Collection.any_of({"field1" => "value1"}, {"field2" =>
"value2"})
criteria = criteria.any_of({"field3" => "value3"}, {"field4" => "value4"})
我得到结果查询,其中所有这些组合成单个$或语句,如
db.collection.find({ $or : [{"field1" : "value1"}, {"field2" : "value2"},
{"field3" : "value3"}, {"field4" : "value4"}]})
使用标准链接实现两个"any_of"的"和"的方法是什么?
我在MongoDB Collection中有几个文件,带有一个字段'name'(这是一个String).
我该如何执行像这样的查询 7 <= name.length <= 14
我一直在努力为具有数组字段的Mongoid模型创建表单.我希望我的表单在数组中的每个条目的文本框中.如果我正在创建一个新记录,则默认将是一个空字段(以及一些javascript以在页面上动态添加新字段).
我使用fields_for搜索了一个解决方案,但似乎更倾向于处理你有一个对象/模型数组的情况,而不是我拥有的情况,这是一个字符串数组.
我将使用一个人和一个电话号码的例子.
class Person
include Mongoid::Document
field :name, :type => String
field :phone_numbers, :type => Array
end
Run Code Online (Sandbox Code Playgroud)
对于控制器,只假设典型的控制器,但在new方法中我用一个空字符串初始化phone_number数组.
这是表单代码:
<%= form_for(@person) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :phone_numbers %><br />
<% @person.phone_numbers.each do |phone_number| %>
<%= text_field_tag "person[phone_numbers][]", phone_number %>
<% end %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
一切正常.有一些我不喜欢的事情.
有没有人对如何实现这个有更好的建议?或者你认为这是正确的吗?
为什么is_a?返回false一Hash类?
例:
value = {"x" => 3, "y" => 2}
puts value.class
puts value.is_a?(Hash)
Run Code Online (Sandbox Code Playgroud)
输出:
Hash
false
Run Code Online (Sandbox Code Playgroud)
我使用Ruby 1.9.2
更新:我的课程的完整来源:
class LatLng
include Mongoid::Fields::Serializable
attr_reader :lat, :lng
def serialize(value)
return if value.nil?
puts value.class
puts value.is_a?(Hash)
if value.is_a?(self.class)
puts "is geopoint" + value.to_json
{'lng' => value.lng.to_f, 'lat' => value.lat.to_f}
elsif value.is_a?(Hash)
hash = value.with_indifferent_access
puts "is hash" + value.to_json
{'lng' => hash['lng'].to_f, 'lat' => hash['lat'].to_f}
end
end
def deserialize(value)
return if value.nil?
value.is_a?(self.class) …Run Code Online (Sandbox Code Playgroud) 我刚开始一个新的rails项目,想通过Mongoid gem使用MongoidDB.按照Mongoid网站上的说明,我向我添加了以下行Gemfile:
gem "mongoid", "~> 2.4"
gem "bson_ext", "~> 1.5"
Run Code Online (Sandbox Code Playgroud)
然后我database.yml按照此处的说明继续删除我的文件.我的application.rb文件现在看起来像这样:
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
require "sprockets/railtie" # Uncomment this line for Rails 3.1+
Run Code Online (Sandbox Code Playgroud)
现在,当我用于rails s在开发中启动我的服务器时,我收到以下错误:
~/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.2.0/lib/rails/railtie/configuration.rb:85:in `method_missing': undefined method `active_record' for #<Rails::Application::Configuration:0x007ff38b20d0b0> (NoMethodError)
Run Code Online (Sandbox Code Playgroud)
我试着找一个解决方案,但似乎还没有人遇到我的问题.难道我做错了什么?这是由最近的Rails 3.2更新引起的吗?
谢谢你的帮助!
更新(1月26日): 根据Dylan Markow的信息,我使用了terminal命令
grep -r active_record config/
Run Code Online (Sandbox Code Playgroud)
并在评论块中将任何引用放入active_record.
我有一个简单的控制器,其中一个动作甚至还没有打到数据库.当我通过浏览器访问操作时,我得到了
ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished):
activerecord (3.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in `retrieve_connection'
activerecord (3.2.0) lib/active_record/connection_adapters/abstract/connection_specification.rb:168:in `retrieve_connection'
activerecord (3.2.0) lib/active_record/connection_adapters/abstract/connection_specification.rb:142:in `connection'
activerecord (3.2.0) lib/active_record/query_cache.rb:67:in `rescue in call' …Run Code Online (Sandbox Code Playgroud) 当使用Mongoid引用的关联时,依赖的detroy和依赖删除之间的差异是什么,因为在它讲述的文档中:
:delete: Delete the child documents.
:destroy: Destroy the child documents.
Run Code Online (Sandbox Code Playgroud) 我正在尝试通过关联的用户名找到记录,该用户名包含在belongs_to关系中,但它不起作用.
文章属于用户用户有很多文章
Article.where(user_id: someid) 工作正常,但我想使用用户名作为参考,存储在Users表中.
Article.includes(:user).where(:username => "erebus")
Article.includes(:user).where("user.username" => "erebus")
Run Code Online (Sandbox Code Playgroud)
我也有 identity_map_enabled: true
Article.includes(:user).inclusions 返回关系详细信息
不起作用,我不理解什么?
我使用Mongoid和rails使用Mongoid.override_database("database_name")与多个数据库进行通信.如何以编程方式查找当前数据库?
关于会话的Mongoid文档:http://mongoid.org/en/moped/docs/driver.html 定义覆盖数据库的方法,但没有定义获取当前数据库的方法.
我在我的Gemfile中注释了sqlite3以及以下行:
gem 'mongoid', '~> 4', github: 'mongoid/mongoid'
gem 'bson_ext'
Run Code Online (Sandbox Code Playgroud)
但是,我一直收到
Specified 'sqlite3' for database adapter, but the gem is not loaded. Add gem "sqlite3" to your Gemfile.
原因似乎是database.yml仍然将sqlite列为数据库.我怎么能让Rails使用生成的mongoid.yml?用mongoid.yml替换database.yml的内容似乎没有办法 - 我得到了
ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter 错误.
它与Rails 4不兼容还是我错过了一些简单的东西?
编辑:我想我变暖了.我已将适配器添加为'mongoid'.这是我的database.yml的内容:
development:
adapter: 'mongoid'
# Configure available database sessions. (required)
sessions:
# Defines the default session. (required)
default:
# Defines the name of the default database that Mongoid can connect to.
# (required).
database: xboxie
# Provides the …Run Code Online (Sandbox Code Playgroud) 我有一个文件,它是在4个领域name,online,like和score.我想通过多个字段和条件来订购带有分页的百万份文件.
示例一些文档:
我的用户文件:
{ "_id": 1, "name": "A", "online": 1, "like": 10, "score": 1 },
{ "_id": 2, "name": "B", "online": 0, "like": 9, "score": 0 },
{ "_id": 3, "name": "C", "online": 0, "like": 8, "score": 1 },
{ "_id": 4, "name": "D", "online": 1, "like": 8, "score": 0 },
{ "_id": 5, "name": "E", "online": 1, "like": 7, "score": 1 },
{ "_id": 6, "name": "F", "online": 0, "like": 10, …Run Code Online (Sandbox Code Playgroud) mongoid ×10
mongodb ×8
ruby ×3
associations ×1
criteria ×1
database ×1
fields-for ×1
mongodb-ruby ×1
mongoid3 ×1