将行旋转到MongoDB中的列 [英] Pivot rows to columns in MongoDB
问题描述
相关问题是在sql中有效地将行转换为列服务器.但是答案是特定于SQL的.
The relevant question is Efficiently convert rows to columns in sql server. But the answer is specific to SQL.
我想要相同的结果,即在MongoDB中将行透视到列而不聚合任何东西(到目前为止).
I want the same result i.e. pivot row to column without aggregating anything (as of now) in MongoDB.
该集合如下所示.这些是facebook页面属性的统计信息:
The collection looks something as below. These are statistics of facebook page properties:
timestamp | propName | propValue
--------------------------------
1371798000000 | page_fans | 100
--------------------------------
1371798000000 | page_posts | 50
--------------------------------
1371798000000 | page_stories | 25
--------------------------------
我需要像这样的答案
timestamp | page_fans | page_posts | page_stories
--------------------------------
1371798000000 | 100 | 50 | 25
--------------------------------
列名是预先确定的.它们不必动态生成.但是问题是如何在MongoDB中实现这一目标.
The column names are pre-determined. They don't have to be generated dynamically. But question is how to achieve this in MongoDB.
我认为聚合对于此目的毫无用处.我需要使用MapReduce吗?但是在那种情况下,我没什么可减少的吗?好吧,另一个选择可能是在代码中获取这些值并以编程语言进行操作,例如Java
I believe aggregation is of no use for this purpose. Do I need to use MapReduce? But in that case I have nothing to reduce I guess? Well another option could be fetching these values in code and do the manipulation in programming language e.g. Java
任何见解都会有所帮助.在此先感谢:)!!!
Any insights would be helpful. Thanks in advance :)!!!
编辑(基于Schaliasos的输入):
EDIT (Based on input from Schaliasos):
输入JSON:
{
"_id" : ObjectId("51cd366644aeac654ecf8f75"),
"name" : "page_storytellers",
"pageId" : "512f993a44ae78b14a9adb85",
"timestamp" : NumberLong("1371798000000"),
"value" : NumberLong(30871),
"provider" : "Facebook"
}
{
"_id" : ObjectId("51cd366644aeac654ecf8f76"),
"name" : "page_fans",
"pageId" : "512f993a44ae78b14a9adb85",
"timestamp" : NumberLong("1371798000000"),
"value" : NumberLong(1291509),
"provider" : "Facebook"
}
{
"_id" : ObjectId("51cd366644aeac654ecf8f77"),
"name" : "page_fan_adds",
"pageId" : "512f993a44ae78b14a9adb85",
"timestamp" : NumberLong("1371798000000"),
"value" : NumberLong(2829),
"provider" : "Facebook"
}
预期的输出JSON:
{
"timestamp" : NumberLong("1371798000000"),
"provider" : "Facebook",
"page_storytellers" : NumberLong(30871),
"page_fans" : NumberLong("1371798000000"),
"page_fan_adds" : NumberLong("1371798000000")
}
推荐答案
我已经使用聚合完成了类似的事情.这可以帮忙吗?
I have done something like this using aggregation. Could this help ?
db.foo.insert({ timestamp: '1371798000000', propName: 'page_fans', propValue: 100})
db.foo.insert({ timestamp: '1371798000000', propName: 'page_posts', propValue: 25})
db.foo.insert({ timestamp: '1371798000000', propName: 'page_stories', propValue: 50})
db.foo.aggregate({ $group: { _id: '$timestamp', result: { $push: { 'propName': '$propName', 'propValue': '$propValue' } }}})
{
"result" : [
{
"_id" : "1371798000000",
"result" : [
{
"propName" : "page_fans",
"propValue" : 100
},
{
"propName" : "page_posts",
"propValue" : 50
},
{
"propName" : "page_stories",
"propValue" : 25
}
]
}
],
"ok" : 1
}
您可能会一直使用$sum
运算符.请参见此处
You may want to use $sum
operator along the way. See here
这篇关于将行旋转到MongoDB中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!