gpt4 book ai didi

node.js - 如何使用mongodb聚合以下数据

转载 作者:太空宇宙 更新时间:2023-11-04 01:25:39 25 4
gpt4 key购买 nike

我有一个集合名称为“sensors”的数据库,它看起来像这样:

{ "_id" : ObjectId("5d4d27a7e1f2cf1d7cba1fe3"), "type" : "thermometer", "value" : 23, "createdAt" : ISODate("2019-08-09T07:58:31.698Z"), "updatedAt" : ISODate("2019-08-09T07:58:31.698Z"), "__v" : 0 }
{ "_id" : ObjectId("5d4d27a7e1f2cf1d7cba1fe4"), "type" : "hygrometer", "value" : 74, "createdAt" : ISODate("2019-08-09T07:58:31.739Z"), "updatedAt" : ISODate("2019-08-09T07:58:31.739Z"), "__v" : 0 }
{ "_id" : ObjectId("5d4d27a8e1f2cf1d7cba1fe5"), "type" : "thermometer", "value" : 25, "createdAt" : ISODate("2019-08-09T07:58:32.551Z"), "updatedAt" : ISODate("2019-08-09T07:58:32.551Z"), "__v" : 0 }
{ "_id" : ObjectId("5d4d27a8e1f2cf1d7cba1fe6"), "type" : "hygrometer", "value" : 86, "createdAt" : ISODate("2019-08-09T07:58:32.584Z"), "updatedAt" : ISODate("2019-08-09T07:58:32.584Z"), "__v" : 0 }
{ "_id" : ObjectId("5d4d27a9e1f2cf1d7cba1fe7"), "type" : "thermometer", "value" : 20, "createdAt" : ISODate("2019-08-09T07:58:33.554Z"), "updatedAt" : ISODate("2019-08-09T07:58:33.554Z"), "__v" : 0 }
{ "_id" : ObjectId("5d4d27a9e1f2cf1d7cba1fe8"), "type" : "hygrometer", "value" : 84, "createdAt" : ISODate("2019-08-09T07:58:33.587Z"), "updatedAt" : ISODate("2019-08-09T07:58:33.587Z"), "__v" : 0 }

我想要做的是查询最后两个文档,温度计和湿度计,然后检查每个文档的值。然后根据我使用 $match 标记设置的条件,我执行我想要的任何操作。

MongoClient.connect('mongodb://localhost', { useNewUrlParser: true }, function (err, client) {
if (err) throw err;
var db = client.db('mongodb');
db.collection('sensors').aggregate(
[
{ $sort: { _id: -1 } },
{ $limit: 1 },
{
$match: {"type": "hygrometer", "value": { "$gt": 60 } }
}
]
).toArray(function(err, item) {
if(err) console.log('error');
if(item.length > 0) console.log('Dehumidifier ON');
else console.log('Dehumidifier OFF');
});

这只是最后一个数据(湿度计)的实现,但我应该如何检查最后两个文档,然后对温度计执行单独的操作,例如“AC ON/OFF”?我在想以某种方式让它检查 item.type == "thermometer"何时执行某些操作,但 item.type 语法似乎不起作用。

最佳答案

请尝试这个:

查询 1:

db.getCollection('sensors').aggregate(
[
{ $sort: { _id: -1 } }, { $group: { _id: '$type', data: { $push: '$$ROOT' } } },
{ $project: { type: '$_id', _id: 0, lastTwo: { $slice: ["$data", 2] } } },
{ $unwind: '$lastTwo' },
{
$match: { $or: [{ 'type': 'hygrometer' }, { "type": "thermometer", "lastTwo.value": { "$gt": 60 } }] }
}
])

查询 2:

  //Check the response below & If you want to group again on type, adding `$group` stage to the above query.    

db.getCollection('sensors').aggregate(
[
{ $sort: { _id: -1 } }, { $group: { _id: '$type', data: { $push: '$$ROOT' } } },
{ $project: { type: '$_id', _id: 0, lastTwo: { $slice: ["$data", 2] } } },
{ $unwind: '$lastTwo' },
{
$match: { $or: [{ 'type': 'hygrometer' }, { "type": "thermometer", "lastTwo.value": { "$gt": 60 } }] }
}, { $group: { _id: '$type', data: { $push: '$$ROOT' } } }, { $project: { type: '$_id', _id: 0, data: 1 } }
])

Output 1中,您可以简单地检查特定类型的对象是否存在,并在Output 2中的代码中执行某些操作(Vs),您必须检查类型和数据数组是否为空。

示例数据:

/* 1 */
{
"_id" : ObjectId("5d4d27a7e1f2cf1d7cba1fe3"),
"type" : "thermometer",
"value" : 23,
"createdAt" : ISODate("2019-08-09T07:58:31.698Z"),
"updatedAt" : ISODate("2019-08-09T07:58:31.698Z"),
"__v" : 0
}

/* 2 */
{
"_id" : ObjectId("5d4d27a7e1f2cf1d7cba1fe4"),
"type" : "hygrometer",
"value" : 74,
"createdAt" : ISODate("2019-08-09T07:58:31.739Z"),
"updatedAt" : ISODate("2019-08-09T07:58:31.739Z"),
"__v" : 0
}

/* 3 */
{
"_id" : ObjectId("5d4d27a8e1f2cf1d7cba1fe5"),
"type" : "thermometer",
"value" : 251,
"createdAt" : ISODate("2019-08-09T07:58:32.551Z"),
"updatedAt" : ISODate("2019-08-09T07:58:32.551Z"),
"__v" : 0
}

/* 4 */
{
"_id" : ObjectId("5d4d27a8e1f2cf1d7cba1fe6"),
"type" : "hygrometer",
"value" : 86,
"createdAt" : ISODate("2019-08-09T07:58:32.584Z"),
"updatedAt" : ISODate("2019-08-09T07:58:32.584Z"),
"__v" : 0
}

/* 5 */
{
"_id" : ObjectId("5d4d27a9e1f2cf1d7cba1fe7"),
"type" : "thermometer",
"value" : 20,
"createdAt" : ISODate("2019-08-09T07:58:33.554Z"),
"updatedAt" : ISODate("2019-08-09T07:58:33.554Z"),
"__v" : 0
}

/* 6 */
{
"_id" : ObjectId("5d4d27a9e1f2cf1d7cba1fe8"),
"type" : "hygrometer",
"value" : 84,
"createdAt" : ISODate("2019-08-09T07:58:33.587Z"),
"updatedAt" : ISODate("2019-08-09T07:58:33.587Z"),
"__v" : 0
}

输出 1:

/* 1 */
{
"type" : "thermometer",
"lastTwo" : {
"_id" : ObjectId("5d4d27a8e1f2cf1d7cba1fe5"),
"type" : "thermometer",
"value" : 251,
"createdAt" : ISODate("2019-08-09T07:58:32.551Z"),
"updatedAt" : ISODate("2019-08-09T07:58:32.551Z"),
"__v" : 0
}
}

/* 2 */
{
"type" : "hygrometer",
"lastTwo" : {
"_id" : ObjectId("5d4d27a9e1f2cf1d7cba1fe8"),
"type" : "hygrometer",
"value" : 84,
"createdAt" : ISODate("2019-08-09T07:58:33.587Z"),
"updatedAt" : ISODate("2019-08-09T07:58:33.587Z"),
"__v" : 0
}
}

/* 3 */
{
"type" : "hygrometer",
"lastTwo" : {
"_id" : ObjectId("5d4d27a8e1f2cf1d7cba1fe6"),
"type" : "hygrometer",
"value" : 86,
"createdAt" : ISODate("2019-08-09T07:58:32.584Z"),
"updatedAt" : ISODate("2019-08-09T07:58:32.584Z"),
"__v" : 0
}
}

输出 2:

/* 1 */
{
"type" : "hygrometer",
"data" : [
{
"type" : "hygrometer",
"lastTwo" : {
"_id" : ObjectId("5d4d27a9e1f2cf1d7cba1fe8"),
"type" : "hygrometer",
"value" : 84,
"createdAt" : ISODate("2019-08-09T07:58:33.587Z"),
"updatedAt" : ISODate("2019-08-09T07:58:33.587Z"),
"__v" : 0
}
},
{
"type" : "hygrometer",
"lastTwo" : {
"_id" : ObjectId("5d4d27a8e1f2cf1d7cba1fe6"),
"type" : "hygrometer",
"value" : 86,
"createdAt" : ISODate("2019-08-09T07:58:32.584Z"),
"updatedAt" : ISODate("2019-08-09T07:58:32.584Z"),
"__v" : 0
}
}
]
}

/* 2 */
{
"type" : "thermometer",
"data" : [
{
"type" : "thermometer",
"lastTwo" : {
"_id" : ObjectId("5d4d27a8e1f2cf1d7cba1fe5"),
"type" : "thermometer",
"value" : 251,
"createdAt" : ISODate("2019-08-09T07:58:32.551Z"),
"updatedAt" : ISODate("2019-08-09T07:58:32.551Z"),
"__v" : 0
}
}
]
}

根据评论进行更新(如果您需要任何类型的文档,因为文档总是有序的),请尝试以下操作:

db.sensors.aggregate( [ { $sort: { _id: -1 } }, { $limit: 2 }, { $match: { $or: [{ 'type': 'hygrometer' , "value": { "$gt": 60 }}, { "type": "thermometer", "value": { "$gt": 20 } }] } } ] )

或将上述查询中的 $slice: ["$data", 2] 更改为 $slice: ["$data", 1]

关于node.js - 如何使用mongodb聚合以下数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57618860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com