这里是MongoDB新手...
我有一个收藏如下...
> db.students.find({_id:22},{scores:[{type:'exam'}]}).pretty()
{
"_id" : 22,
"scores" : [
{
"type" : "exam",
"score" : 75.04996547553947
},
{
"type" : "quiz",
"score" : 10.23046475899236
},
{
"type" : "homework",
"score" : 96.72520512117761
},
{
"type" : "homework",
"score" : 6.488940333376703
}
]
}
如何通过 mongo shell 只显示测验分数?
最佳答案
您的原始示例中有一些语法可能没有达到您的预期......也就是说,看起来您的意图是只匹配特定类型的分数(示例中的“考试”,“测验”根据您的描述)。
以下是一些使用 MongoDB 2.2 shell 的示例。
$elemMatch 投影您可以使用 $elemMatch projection返回数组中的第一个匹配元素:
db.students.find(
// Search criteria
{ '_id': 22 },
// Projection
{ _id: 0, scores: { $elemMatch: { type: 'exam' } }}
)
结果将是每个文档的数组的匹配元素,例如:
{ "scores" : [ { "type" : "exam", "score" : 75.04996547553947 } ] }
如果您想显示多个匹配值或 reshape 结果文档而不是返回完整匹配的数组元素,您可以使用 Aggregation Framework :
db.students.aggregate(
// Initial document match (uses index, if a suitable one is available)
{ $match: {
'_id': 22, 'scores.type' : 'exam'
}},
// Convert embedded array into stream of documents
{ $unwind: '$scores' },
// Only match scores of interest from the subarray
{ $match: {
'scores.type' : 'exam'
}},
// Note: Could add a `$group` by _id here if multiple matches are expected
// Final projection: exclude fields with 0, include fields with 1
{ $project: {
_id: 0,
score: "$scores.score"
}}
)
这种情况下的结果包括:
{ "result" : [ { "score" : 75.04996547553947 } ], "ok" : 1 }
关于mongodb - 在mongodb中提取子数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13452745/
Rails中有没有一种方法可以提取与路由关联的HTTP动词?例如,给定这样的路线:将“users”匹配到:“users#show”,通过:[:get,:post]我能实现这样的目标吗?users_path.respond_to?(:get)(显然#respond_to不是正确的方法)我最接近的是通过执行以下操作,但它似乎并不令人满意。Rails.application.routes.routes.named_routes["users"].constraints[:request_method]#=>/^GET$/对于上下文,我有一个设置cookie然后执行redirect_to:ba
我有一个.pfx格式的证书,我需要使用ruby提取公共(public)、私有(private)和CA证书。使用shell我可以这样做:#ExtractPublicKey(askforpassword)opensslpkcs12-infile.pfx-outfile_public.pem-clcerts-nokeys#ExtractCertificateAuthorityKey(askforpassword)opensslpkcs12-infile.pfx-outfile_ca.pem-cacerts-nokeys#ExtractPrivateKey(askforpassword)o
我已经开始了:defsplit_array(array,size)index=0results=[]ifsize>0whileindex如果我在[1,2,3,4,5,6]上运行它,比如split_array([1,2,3,4,5,6],3)它将产生这个数组:[[1,2,3],[4,5,6]]。在Ruby1.8.7中是否已经有可用的东西可以做到这一点? 最佳答案 [1,2,3,4,5,6].each_slice(3).to_a#=>[[1,2,3],[4,5,6]]对于1.8.6:require'enumerator'[1,2,3,4
我正在尝试提取方括号内的内容。到目前为止,我一直在使用它,它有效,但我想知道我是否可以直接在正则表达式中使用某些东西,而不是使用这个删除功能。a="Thisissuchagreatday[coolawesome]"a[/\[.*?\]/].delete('[]')#=>"coolawesome" 最佳答案 差不多。a="Thisissuchagreatday[coolawesome]"a[/\[(.*?)\]/,1]#=>"coolawesome"a[/(?"coolawesome"第一个依赖于提取组而不是完全匹配;第二个利用前瞻和
如何获取外部命令的输出并从中提取值?我有这样的东西:stdin,stdout,stderr,wait_thr=Open3.popen3("#{path}/foobar",configfile)if/exit0/=~wait_thr.value.to_srunlog.puts("Foobarexitednormally.\n")puts"Testcompleted."someoutputvalue=stdout.read("TX.*\s+(\d+)\s+")puts"Outputvalue:"+someoutputvalueend我没有在标准输出上使用正确的方法,因为Ruby告诉我它不能
我一直在尝试从csv文件中获取单个列。我已经阅读了文档,http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html但仍然不太了解如何使用它。如果我使用CSV.table,与CSV.read相比,响应速度非常慢。我承认我正在加载的数据集非常大,这正是我只想从中获取单个列的原因。我的请求目前看起来像这样@dataTable=CSV.table('path_to_csv.csv')当我调试时,我得到了的响应#ThedocumentationsaysIshouldbeabletouseby_col(),butwhenItrytooutpu
当我写这篇文章时,我以为我是Ruby巨人:#havingthishashhash={'Portugal'=>1,'France'=>2,'USA'=>3}#country_idcomesfrominputcountry_name=(hash.select{|k,v|v==country_id.to_i}.first||[]).first它确实正确地提取了国家名称,如果找不到国家也不会失败。我对此非常满意。但是我的导师说它可以/应该在可读性、长度和性能方面进行优化!还有什么比这更清晰/更快的呢?请指教 最佳答案 嗯,看来你的导师是对的
是否可以从Ruby中的HTMLblock中提取IMG标签(或只是IMG标签的src属性)?例如,如果我有一个HTMLblock,例如:Loremipsumdolorsitamet,laboreetdoloremagnaaliqua.Duisauteiruredolorinreprehenderitinvoluptatevelitessecillumdoloreeufugiatnullapariatur.我可以通过正则表达式或其他方法只提取IMG标签或该IMG标签的src吗?提前感谢您的任何建议! 最佳答案 使用Nokogiri:re
给定一个Proc对象,是否可以查看其中的代码?例如:p=Proc.new{test=0}我需要的是通过某种方式从已创建的Proc对象中获取字符串“test=0”。 最佳答案 您可以使用ruby2ruby图书馆:>>#testedwith1.8.7>>require"parse_tree"=>true>>require"ruby2ruby"=>true>>require"parse_tree_extensions"=>true>>p=Proc.new{test=0}>>p.to_ruby=>"proc{test=0}"您还可以将此过程
我想知道如何从Apple.p12文件中提取key。根据我有限的理解,.p12文件是X504证书和私钥的组合。我看到我遇到的每个.p12文件都有一个X504证书和至少一个key,在某些情况下有两个key。这是因为每个.p12都有一个Apple开发人员key,有些还有一个额外的key(可能是Appleroot授权key)。我只考虑那些具有两个key的.p12文件是有效的。我的目标是区分具有一个key的.p12文件和具有两个key的.p12文件。到目前为止,我已经使用OpenSSL来检查X504文件和任何.p12的key。例如,我有这段代码可以检查目录中的所有.p12文件:Dir.glob(