mongodb数据在现场的转换

时间:2015-07-20 13:25:45

标签: mongodb aggregation-framework

我的测试集合

{ "_id" : 0, "Animals" : "cat", "activity" : "sleep" }
{ "_id" : 1, "Animals" : "dog", "activity" : "run" }
{ "_id" : 2, "Animals" : "cow", "activity" : "play" }
{ "_id" : 3, "Animals" : "cow", "activity" : "sleep" }
{ "_id" : 4, "Animals" : "cow", "activity" : "run" }
{ "_id" : 5, "Animals" : "dog", "activity" : "play" }
{ "_id" : 6, "Animals" : "cat", "activity" : "run" }

找到动物和活动的唯一值

db.test.distinct("Animals")
[ "cat", "dog", "cow" ]
db.test.distinct("service")
[ "sleep", "run", "play" ]

之后动物==猫 - > {cat:1,dog:0,cow:0}与其他

相同

我想更改此类型格式

{ "_id" : 0, "cat" : 1, "dog" : 0, "cow" : 0, "sleep" : 1, "run" : 0,  "play": 0 }
{ "_id" : 1, "cat" : 0, "dog" : 1, "cow" : 0, "sleep" : 0, "run" : 1,  "play": 0 }
{ "_id" : 2, "cat" : 0, "dog" : 0, "cow" : 1, "sleep" : 0, "run" : 0,  "play": 1 }
{ "_id" : 3, "cat" : 0, "dog" : 0, "cow" : 1, "sleep" : 1, "run" : 0,  "play": 0 }
{ "_id" : 4, "cat" : 0, "dog" : 0, "cow" : 1, "sleep" : 0, "run" : 1,  "play": 0 }
{ "_id" : 5, "cat" : 0, "dog" : 1, "cow" : 0, "sleep" : 0, "run" : 0,  "play": 1 }
{ "_id" : 6, "cat" : 1, "dog" : 0, "cow" : 0, "sleep" : 0, "run" : 1,  "play": 0 }

我该怎么做?

2 个答案:

答案 0 :(得分:3)

我会使用聚合框架执行以下操作:

  • 分组_id
  • 算上动物
  • 计算活动
  • 按升序对结果进行排序
db.test.aggregate([ {
$group: {
_id:    "$_id",
cat:   {$sum: {$cond: [{"$eq": ["$Animals",  "cat"]},   1, 0 ] } },
dog:   {$sum: {$cond: [{"$eq": ["$Animals",  "dog"]},   1, 0 ] } },
cow:   {$sum: {$cond: [{"$eq": ["$Animals",  "cow"]},   1, 0 ] } },
sleep: {$sum: {$cond: [{"$eq": ["$activity", "sleep"]}, 1, 0 ] } },
run:   {$sum: {$cond: [{"$eq": ["$activity", "run"]},   1, 0 ] } },
play:  {$sum: {$cond: [{"$eq": ["$activity", "play"]},  1, 0 ] } }
} }, 
{ $sort: {_id: 1}}
] )

mongo shell的结果看起来就像你要找的那样:

{ "_id" : 0, "cat" : 1, "dog" : 0, "cow" : 0, "sleep" : 1, "run" : 0, "play" : 0 }
{ "_id" : 1, "cat" : 0, "dog" : 1, "cow" : 0, "sleep" : 0, "run" : 1, "play" : 0 }
{ "_id" : 2, "cat" : 0, "dog" : 0, "cow" : 1, "sleep" : 0, "run" : 0, "play" : 1 }
{ "_id" : 3, "cat" : 0, "dog" : 0, "cow" : 1, "sleep" : 1, "run" : 0, "play" : 0 }
{ "_id" : 4, "cat" : 0, "dog" : 0, "cow" : 1, "sleep" : 0, "run" : 1, "play" : 0 }
{ "_id" : 5, "cat" : 0, "dog" : 1, "cow" : 0, "sleep" : 0, "run" : 0, "play" : 1 }
{ "_id" : 6, "cat" : 1, "dog" : 0, "cow" : 0, "sleep" : 0, "run" : 1, "play" : 0 }

答案 1 :(得分:3)

我认为您使用aggregation显示如下:

db.test.aggregate([
    {$group : {
        _id : $_id,
        cat : {$cond : {if: { $eq: [ "$Animals", "cat" ] }, then: 1, else: 0}},
        dog : {$cond : {if: { $eq: [ "$Animals", "dog" ] }, then: 1, else: 0}},
        cow : {$cond : {if: { $eq: [ "$Animals", "cow" ] }, then: 1, else: 0}},
        sleep : {$cond : {if: { $eq: [ "$activity", "sleep" ] }, then: 1, else: 0}},
        run : {$cond : {if: { $eq: [ "$activity", "run" ] }, then: 1, else: 0}},
        play : {$cond : {if: { $eq: [ "$activity", "play" ] }, then: 1, else: 0}}
    }}
]);

此代码段使用$cond聚合运算符+ $eq比较运算符。

更多内容阅读:

http://docs.mongodb.org/manual/reference/operator/aggregation/group/#group-aggregation http://docs.mongodb.org/manual/reference/operator/aggregation/cond/#cond-aggregation http://docs.mongodb.org/manual/core/aggregation-pipeline/