У меня есть 2 связанных коллекции, которые я хочу сделать $lookup
.
- переключатели
{
"_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"relay" : NumberInt(1),
"name" : "Lampu Tengah",
"voltage" : 80.0,
"duration" : null,
"status" : true,
"triggered_by" : ObjectId("5e5fd642fce106005319e884"),
"created_at" : ISODate("2020-04-01T15:41:36.588+0000"),
"updated_at" : ISODate("2020-04-01T22:59:39.261+0000")
}
{
"_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"relay" : NumberInt(2),
"name" : "Kipas Angin",
"voltage" : 100.0,
"duration" : null,
"status" : true,
"triggered_by" : ObjectId("5e5fd642fce106005319e884"),
"created_at" : ISODate("2020-04-01T15:45:48.099+0000"),
"updated_at" : ISODate("2020-04-01T15:45:48.099+0000")
}
power_usage_month {
"_id" : ObjectId("5e87edffffba850e8d72ce27"),
"switch_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"current" : 19.345,
"time_minutes" : NumberInt(123),
"created_at" : ISODate("2020-04-02T15:01:37.521+0000"),
"updated_at" : ISODate("2020-04-02T15:01:37.521+0000")
}
{
"_id" : ObjectId("5e87ee06ffba850e8d72ce28"),
"switch_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"current" : 17.5,
"time_minutes" : NumberInt(123),
"created_at" : ISODate("2020-04-03T20:35:09.870+0000"),
"updated_at" : ISODate("2020-04-03T20:35:09.871+0000")
}
{
"_id" : ObjectId("5e87ee0cffba850e8d72ce29"),
"switch_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"current" : 19.345,
"time_minutes" : NumberInt(124),
"created_at" : ISODate("2020-04-04T01:45:00.000+0000"),
"updated_at" : ISODate("2020-04-04T01:45:00.000+0000")
}
{
"_id" : ObjectId("5e87ee13ffba850e8d72ce2a"),
"switch_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"current" : 17.5,
"time_minutes" : NumberInt(124),
"created_at" : ISODate("2020-04-04T01:45:00.000+0000"),
"updated_at" : ISODate("2020-04-04T01:45:00.000+0000")
}
{
"_id" : ObjectId("5e87ee18ffba850e8d72ce2b"),
"switch_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"current" : 19.345,
"time_minutes" : NumberInt(125),
"created_at" : ISODate("2020-04-04T01:45:00.000+0000"),
"updated_at" : ISODate("2020-04-04T01:45:00.000+0000")
}
{
"_id" : ObjectId("5e87ee20ffba850e8d72ce2c"),
"switch_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"current" : 17.5,
"time_minutes" : NumberInt(125),
"created_at" : ISODate("2020-04-04T01:45:00.000+0000"),
"updated_at" : ISODate("2020-04-04T01:45:00.000+0000")
}
{
"_id" : ObjectId("5e87ee26ffba850e8d72ce2d"),
"switch_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"current" : 19.345,
"time_minutes" : NumberInt(126),
"created_at" : ISODate("2020-04-04T01:45:00.000+0000"),
"updated_at" : ISODate("2020-04-04T01:45:00.000+0000")
}
{
"_id" : ObjectId("5e87ee2dffba850e8d72ce2e"),
"switch_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"current" : 17.5,
"time_minutes" : NumberInt(126),
"created_at" : ISODate("2020-04-04T01:45:00.000+0000"),
"updated_at" : ISODate("2020-04-04T01:45:00.000+0000")
}
Теперь я хочу "присоединиться" к этим коллекциям, используя $ lookup и $project
поле, которое я хочу показать, но, очевидно, $project
значение не будет отображаться после того, как я $group
. Вот мой запрос
db.getCollection("power_usages_month").aggregate(
[
{
"$project" : {
"_id" : NumberInt(0),
"power_usages_month" : "$$ROOT"
}
},
{
"$lookup" : {
"localField" : "power_usages_month.switch_id",
"from" : "switches",
"foreignField" : "_id",
"as" : "switches"
}
},
{
"$unwind" : {
"path" : "$switches",
"preserveNullAndEmptyArrays" : false
}
},
{
"$group" : {
"_id" : "$power_usages_month.switch_id",
"sum_current" : {
"$sum" : "$power_usages_month.current"
}
}
},
{
"$project" : {
"switch_id" : "$_id",
"device_id" : "$switches.device_id",
"sum_current" : "$sum_current",
"voltage" : "$switches.voltage",
}
}
],
{
"allowDiskUse" : true
}
);
"device_id" : "$switches.device_id",
и "voltage" : "$switches.voltage"
не могут отображаться, как ожидалось в зависимости от результата:
{
"_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"switch_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"sum_current" : 70.0
}
{
"_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"switch_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"sum_current" : 77.38
}
Как это могло быть? Пожалуйста, сообщите мне ошибку запроса ..
Редактировать: Вот мой желаемый результат
{
"_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"switch_id" : ObjectId("5e8454bc95c85ca0c33a9463"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"sum_current" : 70.0,
"voltage" : 80.0
}
{
"_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"switch_id" : ObjectId("5e8453c095c85ca0c33a9461"),
"device_id" : ObjectId("5e7d83efd62c242a11e3ca5e"),
"sum_current" : 77.38,
"voltage" : 100.0
}