我完全被难过了!
我在名为“Gene”的集合中有以下文档结构,我感兴趣的子文档是“DrugDBI”。我希望返回的字段是“drug_name”而没有别的;这里是“AAA”和“LLL”。
{
"ADR": [
{
"adverse_type": "YYY",
}
],
"DrugDBI": [
{
"cancer_type": "XXX",
"drug_name": "AAAA",
},
{
"cancer_type": "1405-87-4",
"drug_name": "LLL",
}
]
"_id": "A2M",
"ts": { "$date": 1395239463625 }
}
和
我需要从子文档中提取一个字段(即“drug_name”),同时通过id查询;
例如;
我的查询ID是@gene_v;这是“A2M”
@gened = DB['Gene'].find({_id:@gene_v,fields:[{"DrugDBI" => {"$elemMatch" => {"$in" => "drug_name" } }}]}).to_a
puts "Drugs related to genes -> <#{@gened}>"
这显然不会返回任何价值。我感谢任何帮助。
当前输出为;
Drugs related to genes -> <[]>
答案 0 :(得分:0)
MongoDB只提供&#34;文件&#34;而不是任意单个字段值,也不是字段值数组。 因此,您始终必须按键从文档中提取单个值。 但是,MongoDB和文档的主要优势是与编程语言结构相匹配, 因此授权您使用您的编程语言。 您应该将编程语言视为首选工具。
MongoDB查找查询仅允许简单地投影/选择顶级字段。 您可以使用聚合框架进行更复杂的提取和转换, 但我建议你先用你的编程语言。 我希望以下测试能够为您说明所有这些。
test.rb
require 'mongo'
require 'json'
require 'test/unit'
class MyTest < Test::Unit::TestCase
def setup
@coll = Mongo::MongoClient.new['test']['Gene']
doc = JSON.parse <<-EOT
{ "ADR" : [{"adverse_type" : "YYY"}],
"DrugDBI" : [{"cancer_type" : "XXX", "drug_name" : "AAAA"}, {"cancer_type" : "1405-87-4", "drug_name" : "LLL"}],
"_id" : "A2M",
"ts" : {"$date" : 1395239463625}
}
EOT
@coll.remove
@coll.insert(doc)
end
test "extract cancer drugs" do
@gene_v = "A2M"
doc = @coll.find({_id:@gene_v},fields:["DrugDBI"]).to_a.first
@gened = doc["DrugDBI"].collect{|cancer_drug| cancer_drug["drug_name"]}
assert_equal(["AAAA", "LLL"], @gened)
puts "Drugs related to genes -> <#{@gened}>"
doc = @coll.aggregate([
{"$match" => {"_id" => @gene_v}},
{"$unwind" => "$DrugDBI"},
{"$project" => {"drug_name" => "$DrugDBI.drug_name"}},
{"$group" => {"_id" => "$_id", "drug_name" => {"$push" => "$drug_name"}}}
]).to_a.first
@gened = doc["drug_name"]
assert_equal(["AAAA", "LLL"], @gened)
puts "Drugs related to genes -> <#{@gened}>"
end
end
$ ruby test.rb
Loaded suite test
Started
Drugs related to genes -> <["AAAA", "LLL"]>
Drugs related to genes -> <["AAAA", "LLL"]>
.
Finished in 0.010455 seconds.
1 tests, 2 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications