Mongo Ruby 指定字段值上的驱动程序 #find()
Mongo Ruby Driver #find() on Specified field Values
- Ruby:
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
- Ruby宝石:
mongo (2.0.4)
我需要帮助使用 gem 查询 MongoDB 数据库并更新相应的字段。
编辑: 我正在尝试遍历 Mongo 数据库的文档,提取这些文档中特定字段的值,并在稍后更新它们脚本。
目标
查询数据库中字段partner_id
为"partner"且字段state
为"provisioned"且[=52=的文档] 只有 _id
和 config
字段下的值。
- 在此之后,我将遍历每个文档、生成密码并更新另一个数据库。
用每个文档 config
字段新生成的密码更新数据库。
我已经无计可施了,因为我已经看到了大约六种不同的语法编写方式,而且文档对我的帮助不大,除非我已经知道如何做这些事情。如有任何帮助,我们将不胜感激!
#!/usr/bin/env ruby
require 'json'
require 'net/http'
require 'mongo'
# Fetch the addons database URI and connect.
db_uri = ENV['DATABASE_URI']
client = Mongo::Client.new(db_uri)
# Connect to the needed collection and pull down each document to be looped over individually.
# **Having trouble getting this to work. The result is just '= []' - don't know what I'm doing wrong.
client[:collection].find("partner_id" => "partner", "state" => "provisioned", :fields => ["_id", "config"]).each {
# Need something here to pull down the values from each document's '_id' and 'config' fields and assign them to variables.
user_id =
user_config =
user_config = JSON.parse(user_config)
# ...generating password and updating other database...
# Convert the Hash of the user's new configuration into JSON, and update the original database with it.
# Not sure if any of this is correct. When querying to check, the database doesn't seem to be updated.
user_config = user_config.to_json
client[:collection].update(
{"_id" => user_id},
{'$set' => {
"config" => user_config
}
}
)
}
end
return
您没有找到任何东西,因为:
:fields => ["_id", "config"]
find
的参数没有指定您想要的字段,find
只是将其视为要查找的第三个文档字段。您的文档可能没有名为 field
的字段,其值是这些字符串的数组,因此查询静静地什么也找不到。
如果你想限制你的查询,你需要使用projection
:
client[:collection].find("partner_id" => "partner", "state" => "provisioned")
.projection('_id' => 1, 'config' => 1)
.each { |doc| ... }
然后在 each
块内 doc
将是一个哈希,所以你可以说:
user_id = doc['user_id']
user_config = doc['user_config']
如果我没看错你的代码,user_config
应该已经是哈希了,所以你可能不需要自己解析它。
- Ruby:
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
- Ruby宝石:
mongo (2.0.4)
我需要帮助使用 gem 查询 MongoDB 数据库并更新相应的字段。
编辑: 我正在尝试遍历 Mongo 数据库的文档,提取这些文档中特定字段的值,并在稍后更新它们脚本。
目标查询数据库中字段
partner_id
为"partner"且字段state
为"provisioned"且[=52=的文档] 只有_id
和config
字段下的值。- 在此之后,我将遍历每个文档、生成密码并更新另一个数据库。
用每个文档
config
字段新生成的密码更新数据库。
我已经无计可施了,因为我已经看到了大约六种不同的语法编写方式,而且文档对我的帮助不大,除非我已经知道如何做这些事情。如有任何帮助,我们将不胜感激!
#!/usr/bin/env ruby
require 'json'
require 'net/http'
require 'mongo'
# Fetch the addons database URI and connect.
db_uri = ENV['DATABASE_URI']
client = Mongo::Client.new(db_uri)
# Connect to the needed collection and pull down each document to be looped over individually.
# **Having trouble getting this to work. The result is just '= []' - don't know what I'm doing wrong.
client[:collection].find("partner_id" => "partner", "state" => "provisioned", :fields => ["_id", "config"]).each {
# Need something here to pull down the values from each document's '_id' and 'config' fields and assign them to variables.
user_id =
user_config =
user_config = JSON.parse(user_config)
# ...generating password and updating other database...
# Convert the Hash of the user's new configuration into JSON, and update the original database with it.
# Not sure if any of this is correct. When querying to check, the database doesn't seem to be updated.
user_config = user_config.to_json
client[:collection].update(
{"_id" => user_id},
{'$set' => {
"config" => user_config
}
}
)
}
end
return
您没有找到任何东西,因为:
:fields => ["_id", "config"]
find
的参数没有指定您想要的字段,find
只是将其视为要查找的第三个文档字段。您的文档可能没有名为 field
的字段,其值是这些字符串的数组,因此查询静静地什么也找不到。
如果你想限制你的查询,你需要使用projection
:
client[:collection].find("partner_id" => "partner", "state" => "provisioned")
.projection('_id' => 1, 'config' => 1)
.each { |doc| ... }
然后在 each
块内 doc
将是一个哈希,所以你可以说:
user_id = doc['user_id']
user_config = doc['user_config']
如果我没看错你的代码,user_config
应该已经是哈希了,所以你可能不需要自己解析它。