这是我在文本模式下的示例 MongoDB
我想检索每个用户的数据。我也想要 _id,因为在一个完整的文档中它有几个结构相似的 _id。
我尝试使用 unwind 运算符,因为该对象包含这样的嵌套数组:
db.getCollection('topic_stats_2').aggregate([{ $unwind : "$usages.type.users.text" }, { $unwind : "$usages.type.users.stats.xyz1" }, { $unwind : "$usages.type.users.stats.xyz2" }, { $unwind : "$usages.type.users.stats.xyz3" }, { $unwind : "$usages.type.users.xyz4" }, { $unwind : "$usages.type.users.xyz5" }])
但结果为零。
我想要这样的表格格式的结果。我知道该表将包含大量冗余数据。但这是我想要的。
_id | count | xyz4 | xyz5 | xyz1 | xyz2 | xyz3 | text | type |
| | | | | | | | |
| | | | | | | | |
/* 1 */
{
"_id" : “photo",
"count" : 236,
"usages" : [
{
"type" : 1,
"users" : [
{
"text" : “jkncfkjfdn",
"stats" : {
“xyz1" : 6,
“xyz2" : 1,
“xyz3" : 1194
},
“xyz4" : "julius babao",
“xyz5" : "juLiusbabao"
},
{
"text" : “fcnf",
"stats" : {
“xyz1" : 9,
“xyz2" : 6,
“xyz3" : 1199
},
“xyz4" : "Dman",
“xyz5" : "DmanTheDesigner"
},
{
"text" : “dckejsndc",
"stats" : {
“xyz1" : 1,
“xyz2" : 0,
“xyz3" : 1200
},
“xyz4" : "EastmanHouse",
“xyz5" : "EastmanHouse"
}
]
},
{
"type" : 2,
"users" : [
{
"text" : “msdnc",
"stats" : {
“xyz1" : 1,
“xyz2" : 1,
“xyz3" : 1168
},
“xyz4" : "Shayne",
“xyz5" : "RKTay"
},
{
"text" : “kfjnvfv",
"stats" : {
“xyz1" : 0,
“xyz2" : 0,
“xyz3" : 523
},
“xyz4" : "andy stitches",
“xyz5" : "myproudmendes"
},
{
"text" : “jkopoiuyt",
"stats" : null,
“xyz4" : "jm",
“xyz5" : "jihannelayosa"
}
]
},
{
"type" : 3,
"users" : [
{
"text" : “opted",
"stats" : {
“xyz1" : 58,
“xyz2" : 32,
“xyz3" : 1192
},
“xyz4" : "♪♫Lil Darryl♫♪",
“xyz5" : "LilDarryl301"
},
{
"text" : "Cloud 9",
"stats" : {
“xyz1" : 1,
“xyz2" : 1,
“xyz3" : 1171
},
“xyz4" : "FGN",
“xyz5" : "pretty_brown66"
},
{
"text" : "Cloud 9",
"stats" : {
“xyz1" : 0,
“xyz2" : 0,
“xyz3" : 997
},
“xyz4" : "Travis Porter Jr .",
“xyz5" : "AyoTravo"
}
]
},
{
"type" : 4,
"users" : [
{
"text" : “while",
"stats" : {
“xyz1" : 1,
“xyz2" : 1,
“xyz3" : 1200
},
“xyz4" : "LEGO Darth Vader",
“xyz5" : "LegoDarthVader"
},
{
"text" : “xjw",
"stats" : {
“xyz1" : 1,
“xyz2" : 1,
“xyz3" : 1198
},
“xyz4" : "The Brothers Brick",
“xyz5" : "BrothersBrick"
},
{
"text" : “pol",
"stats" : {
“xyz1" : 1,
“xyz2" : 1,
“xyz3" : 1197
},
“xyz4" : "BYTES & BRICKS",
“xyz5" : "lego_bb"
}
]
},
{
"type" : 5,
"users" : [
{
"text" : “qtwqyw",
"stats" : {
“xyz1" : 1,
“xyz2" : 1,
“xyz3" : 1155
},
“xyz4" : "Kell_1976",
“xyz5" : "LuvsMyMunchkie"
},
{
"text" : “ytyty",
"stats" : {
“xyz1" : 12,
“xyz2" : 4,
“xyz3" : 1200
},
“xyz4" : "carriewildes",
“xyz5" : "carriewildes"
},
{
"text" : "from the high.",
"stats" : {
“xyz1" : 0,
“xyz2" : 0,
“xyz3" : 1067
},
“xyz4" : "jake☄",
“xyz5" : "w0rshiptheking"
}
]
}
]
}
有人可以帮我解决这个问题吗?
最佳答案
您应用 $unwind 的方式 运算符是错误的,因为它仅作用于数组字段,而您将其应用于非数组字段。您可以运行以下聚合管道对数组字段进行非规范化并获得所需的结构:
db.getCollection('topic_stats_2').aggregate([
{ "$unwind": "$usages" },
{ "$unwind": "$usages.users" },
{
"$project": {
"count": 1,
"xyz4": "$usages.users.xyz4",
"xyz5": "$usages.users.xyz5",
"xyz1": "$usages.users.stats.xyz1",
"xyz2": "$usages.users.stats.xyz2",
"xyz3": "$usages.users.stats.xyz3",
"text": "$usages.users.text",
"type": "$usages.type"
}
}
])
关于arrays - 查询嵌套文档 mongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36794894/
我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[
通过rubykoans.com,我在about_array_assignment.rb中遇到了这两段代码你怎么知道第一个是非并行赋值,第二个是一个变量的并行赋值?在我看来,除了命名差异之外,代码几乎完全相同。4deftest_non_parallel_assignment5names=["John","Smith"]6assert_equal["John","Smith"],names7end45deftest_parallel_assignment_with_one_variable46first_name,=["John","Smith"]47assert_equal'John
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc
我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的
这个问题在这里已经有了答案:Arraysmisbehaving(1个回答)关闭6年前。是否应该这样,即我误解了,还是错误?a=Array.new(3,Array.new(3))a[1].fill('g')=>[["g","g","g"],["g","g","g"],["g","g","g"]]它不应该导致:=>[[nil,nil,nil],["g","g","g"],[nil,nil,nil]]
我有一个名为posts的模型,它有很多附件。附件模型使用回形针。我制作了一个用于创建附件的独立模型,效果很好,这是此处说明的View(https://github.com/thoughtbot/paperclip):@attachment,:html=>{:multipart=>true}do|form|%>posts中的嵌套表单如下所示:prohibitedthispostfrombeingsaved:@attachment,:html=>{:multipart=>true}do|at_form|%>附件记录已创建,但它是空的。文件未上传。同时,帖子已成功创建...有什么想法吗?