我将MongoDb中的数据保存为以下格式:
-std=gnu++11
我想要实现的是每5秒获取一次记录。所以我期望的输出应该是
/* 1 */
{
"_id" : ObjectId("5a40f49743bf8220c865cbaf"),
"TIMESTAMP" : ISODate("2017-01-08T11:14:11.000Z"),
"WIZ_VAR_10" : 710.0,
"WIZ_VAR_11" : 910.0,
"WIZ_VAR_12" : 910.0,
"TIMESTAMP_EP" : 1483854251.0
}
/* 2 */
{
"_id" : ObjectId("5a40f49743bf8220c865cbb0"),
"TIMESTAMP" : ISODate("2017-01-08T11:14:12.000Z"),
"WIZ_VAR_10" : 720.0,
"WIZ_VAR_11" : 920.0,
"WIZ_VAR_12" : 920.0,
"TIMESTAMP_EP" : 1483854252.0
}
/* 3 */
{
"_id" : ObjectId("5a40f49743bf8220c865cbb1"),
"TIMESTAMP" : ISODate("2017-01-08T11:14:13.000Z"),
"WIZ_VAR_10" : 730.0,
"WIZ_VAR_11" : 930.0,
"WIZ_VAR_12" : 930.0,
"TIMESTAMP_EP" : 1483854253.0
}
/* 4 */
{
"_id" : ObjectId("5a40f49743bf8220c865cbb2"),
"TIMESTAMP" : ISODate("2017-01-08T11:14:14.000Z"),
"WIZ_VAR_10" : 740.0,
"WIZ_VAR_11" : 940.0,
"WIZ_VAR_12" : 940.0,
"TIMESTAMP_EP" : 1483854254.0
}
/* 5 */
{
"_id" : ObjectId("5a40f49743bf8220c865cbb3"),
"TIMESTAMP" : ISODate("2017-01-08T11:14:15.000Z"),
"WIZ_VAR_10" : 750.0,
"WIZ_VAR_11" : 950.0,
"WIZ_VAR_12" : 950.0,
"TIMESTAMP_EP" : 1483854255.0
}
/* 6 */
{
"_id" : ObjectId("5a40f49743bf8220c865cbb4"),
"TIMESTAMP" : ISODate("2017-01-08T11:14:16.000Z"),
"WIZ_VAR_10" : 760.0,
"WIZ_VAR_11" : 960.0,
"WIZ_VAR_12" : 960.0,
"TIMESTAMP_EP" : 1483854256.0
}
/* 7 */
{
"_id" : ObjectId("5a40f49743bf8220c865cbb5"),
"TIMESTAMP" : ISODate("2017-01-08T11:14:17.000Z"),
"WIZ_VAR_10" : 770.0,
"WIZ_VAR_11" : 970.0,
"WIZ_VAR_12" : 970.0,
"TIMESTAMP_EP" : 1483854257.0
}
/* 8 */
{
"_id" : ObjectId("5a40f49743bf8220c865cbb6"),
"TIMESTAMP" : ISODate("2017-01-08T11:14:18.000Z"),
"WIZ_VAR_10" : 780.0,
"WIZ_VAR_11" : 980.0,
"WIZ_VAR_12" : 980.0,
"TIMESTAMP_EP" : 1483854258.0
}
/* 9 */
{
"_id" : ObjectId("5a40f49743bf8220c865cbb7"),
"TIMESTAMP" : ISODate("2017-01-08T11:14:19.000Z"),
"WIZ_VAR_10" : 790.0,
"WIZ_VAR_11" : 990.0,
"WIZ_VAR_12" : 990.0,
"TIMESTAMP_EP" : 1483854259.0
}
/* 10 */
{
"_id" : ObjectId("5a40f49743bf8220c865cbb8"),
"TIMESTAMP" : ISODate("2017-01-08T11:14:20.000Z"),
"WIZ_VAR_10" : 800.0,
"WIZ_VAR_11" : 0.0,
"WIZ_VAR_12" : 0.0,
"TIMESTAMP_EP" : 1483854260.0
}
/* 11 */
{
"_id" : ObjectId("5a40f49743bf8220c865cbb9"),
"TIMESTAMP" : ISODate("2017-01-08T11:14:21.000Z"),
"WIZ_VAR_10" : 810.0,
"WIZ_VAR_11" : 10.0,
"WIZ_VAR_12" : 10.0,
"TIMESTAMP_EP" : 1483854261.0
}
/* 12 */
{
"_id" : ObjectId("5a40f49743bf8220c865cbba"),
"TIMESTAMP" : ISODate("2017-01-08T11:14:22.000Z"),
"WIZ_VAR_10" : 820.0,
"WIZ_VAR_11" : 20.0,
"WIZ_VAR_12" : 20.0,
"TIMESTAMP_EP" : 1483854262.0
}
所以本质上我想每隔一段时间就获取一次记录。我检查了聚合框架,但找不到相同的确切解决方案。 我们非常感谢您的帮助。
答案 0 :(得分:1)
您可以使用$second运算符,该运算符从日期(0-60)
起算秒,然后用$mod取模5
(间隔5
秒)。在您的情况下,您想从11
开始,因此应该检查模数是$eq到1
。要直接在$match
中使用这些运算符,您需要$expr(在MongoDB v 3.6中可用):
db.col.aggregate([
{
$match: {
$expr: {
$eq: [ { $mod: [ { $second: "$TIMESTAMP" }, 5 ] }, 1 ]
}
}
}
])
如果您使用较低的MongoDB版本,则可以尝试使用$redact:
db.col.aggregate([
{
$redact: {
$cond: {
if: { $eq: [ { $mod: [ { $second: "$TIMESTAMP" }, 5 ] }, 1 ] },
then: "$$KEEP",
else: "$$PRUNE"
}
}
}
])