Mongodb时间序列操作和生成

Mongodb时间序列操作和生成,mongodb,time-series,Mongodb,Time Series,我有一个Mongodb收藏,里面有这样的文档: { "_id" : ObjectId("53cb898bed4bd6c24ae07a9f"), "account" : "C1" "created_on" : ISODate("2014-10-01T01:23:00.000Z") "value" : 253 } 及 C1和C2每分钟有一份文件 我想为另一个帐户“C0”生成数据,它将等于:(C2-C1)*0.25 因此,目标是为收集中的每分钟生成数据 据你所说,在mongo shell有可能做到这

我有一个Mongodb收藏,里面有这样的文档:

{
"_id" : ObjectId("53cb898bed4bd6c24ae07a9f"),
"account" : "C1"
"created_on" : ISODate("2014-10-01T01:23:00.000Z")
"value" : 253
}

C1和C2每分钟有一份文件

我想为另一个帐户“C0”生成数据,它将等于:(C2-C1)*0.25 因此,目标是为收集中的每分钟生成数据

据你所说,在mongo shell有可能做到这一点吗


非常感谢:)

解决此问题的逻辑如下:

 a) group all the records by created_on date.
 b) get the value of both the documents in each group.
 c) calculate the difference the C2 and C1 documents for each group.
 d) In case one of the documents is missing difference 
    would be the value of the existing document.
 d) project a document with value as (difference*.25) in each group.
 e) insert the projected document to the collection.
db.time.aggregate([ {
    $match : {
        "account" : {
            $in : [ "C1", "C2" ]
        }
    }
}, {
    $group : {
        "_id" : {
            "created_on" : "$created_on",
            "account" : "$account"
        },
        "created_on" : {
            $first : "$created_on"
        },
        "values" : {
            $sum : "$value"
        }
    }
}, {
    $group : {
        "_id" : "$created_on",
        "first" : {
            $first : "$values"
        },
        "second" : {
            $last : "$values"
        },
        "count" : {
            $sum : 1
        }
    }
}, {
    $project : {
        "_id" : 0,
        "value" : {
            $multiply : [ {
                $cond : [ {
                    $lte : [ "$count", 1 ]
                }, "$first", {
                    $subtract : [ "$first", "$second" ]
                } ]
            }, 0.25 ]
        },
        "created_on" : "$_id",
        "account" : {
            $literal : "C0"
        }
    }
} ]).forEach(function(doc) {
    doc.value = Math.abs(doc.value);
    db.time.insert(doc);
});
我想提出两个解决方案,第一个是基于你的假设

C1和C2每分钟有一份文件

因此,对于在
时间创建的每个
文档,
只有两个
文档,
C1
C2

db.time.aggregate([ {
    $match : {
        "account" : {
            $in : [ "C1", "C2" ]
        }
    }
}, {
    $group : {
        "_id" : "$created_on",
        "first" : {
            $first : "$value"
        },
        "second" : {
            $last : "$value"
        },
        "count" : {
            $sum : 1
        }
    }
}, {
    $project : {
        "_id" : 0,
        "value" : {
            $multiply : [ {
                $cond : [ {
                    $lte : [ "$count", 1 ]
                }, "$first", {
                    $subtract : [ "$first", "$second" ]
                } ]
            }, 0.25 ]
        },
        "created_on" : "$_id",
        "account" : {
            $literal : "C0"
        }
    }
} ]).forEach(function(doc) {
    doc.value = Math.abs(doc.value);
    db.time.insert(doc);
});
第二种解决方案基于实时场景。对于在
时间创建的特定
,可以有
'n'
个C1文档和
'm'
个具有不同值的
C2
文档,但对于在
时间创建的特定
文档,我们只需要一个
'C0'
文档来表示差异。您需要一个额外的
$group
管道操作符,如下所示:

 a) group all the records by created_on date.
 b) get the value of both the documents in each group.
 c) calculate the difference the C2 and C1 documents for each group.
 d) In case one of the documents is missing difference 
    would be the value of the existing document.
 d) project a document with value as (difference*.25) in each group.
 e) insert the projected document to the collection.
db.time.aggregate([ {
    $match : {
        "account" : {
            $in : [ "C1", "C2" ]
        }
    }
}, {
    $group : {
        "_id" : {
            "created_on" : "$created_on",
            "account" : "$account"
        },
        "created_on" : {
            $first : "$created_on"
        },
        "values" : {
            $sum : "$value"
        }
    }
}, {
    $group : {
        "_id" : "$created_on",
        "first" : {
            $first : "$values"
        },
        "second" : {
            $last : "$values"
        },
        "count" : {
            $sum : 1
        }
    }
}, {
    $project : {
        "_id" : 0,
        "value" : {
            $multiply : [ {
                $cond : [ {
                    $lte : [ "$count", 1 ]
                }, "$first", {
                    $subtract : [ "$first", "$second" ]
                } ]
            }, 0.25 ]
        },
        "created_on" : "$_id",
        "account" : {
            $literal : "C0"
        }
    }
} ]).forEach(function(doc) {
    doc.value = Math.abs(doc.value);
    db.time.insert(doc);
});

如何生成这两个文档(C1、C2)?我没有生成它们,我只是下载了一个完全由分层系统管理的数据库。