sql >> Database >  >> NoSQL >> MongoDB

MongoDB:Hoe een tijdreeks opvragen met onvolledige gegevens?

Hier is aggregatie met de aanpak die ik in mijn eerste opmerking had genoemd:

db.collection.aggregate( [
  { 
      $sort: { timestamp: 1 } 
  },
  { 
      $group: { 
           _id: null,
           docs: { $push: { timestamp: "$timestamp", device_id: "$device_id", temp: "$temp", missing: false } },
           device_id: { $first: "$device_id" },
           start: { $first: { $toInt: { $divide: [ { "$toLong": "$timestamp" }, 1000 ] } } }, 
           end: { $last: { $toInt: { $divide: [ { "$toLong": "$timestamp" }, 1000 ] } } }
      } 
  },
  { 
      $addFields: {
           docs: {
               $map: {
                    input: { $range: [ { $toInt: "$start" }, { $add: [ { $toInt: "$end" }, 900 ] }, 900 ] }, 
                    as: "ts",
                    in: {
                        ts_exists: { $arrayElemAt: [ 
                                              { $filter: { 
                                                      input: "$docs", as: "d", 
                                                      cond: { $eq: [ { $toInt: { $divide: [ { "$toLong": "$$d.timestamp" }, 1000 ] } },
                                                                      "$$ts"
                                                             ] }
                                               }}, 
                                     0 ] },
                         ts: "$$ts"
                    }
              }
          }
      }
  },
  { 
      $unwind: "$docs" 
  },
  { 
      $addFields: { 
          docs: { 
              $ifNull: [ "$docs.ts_exists", { timestamp: { $toDate: { $multiply: [ "$docs.ts", 1000 ] } }, 
                                              temp: 0, device_id: "$device_id", missing: true 
                                             } 
                       ] 
          }
      }
  },
  { 
      $replaceRoot: { newRoot: "$docs" } 
  }
] ).pretty()

Met behulp van de volgende invoerdocumenten :

{"device_id": "ABC","temp": 12,"timestamp": ISODate("2020-01-04T17:45:00.000+00:00") },
{"device_id": "ABC","temp": 10,"timestamp": ISODate("2020-01-04T18:00:00.000+00:00") },
{"device_id": "ABC","temp": 4,"timestamp": ISODate("2020-01-04T18:30:00.000+00:00") },
{"device_id": "ABC","temp": 23,"timestamp": ISODate("2020-01-04T18:45:00.000+00:00") }

Het resultaat :

{
        "timestamp" : ISODate("2020-01-04T17:45:00Z"),
        "device_id" : "ABC",
        "temp" : 12,
        "missing" : false
}
{
        "timestamp" : ISODate("2020-01-04T18:00:00Z"),
        "device_id" : "ABC",
        "temp" : 10,
        "missing" : false
}
{
        "timestamp" : ISODate("2020-01-04T18:15:00Z"),
        "temp" : 0,
        "device_id" : "ABC",
        "missing" : true
}
{
        "timestamp" : ISODate("2020-01-04T18:30:00Z"),
        "device_id" : "ABC",
        "temp" : 4,
        "missing" : false
}
{
        "timestamp" : ISODate("2020-01-04T18:45:00Z"),
        "device_id" : "ABC",
        "temp" : 23,
        "missing" : false
}



  1. Hoe kan ik een record drom-database ophalen in de bewerkingsweergave in Laravel?

  2. De meest efficiënte manier om meerdere hashes in Redis te krijgen?

  3. Loggen met winston-mongodb en express-winston

  4. Databasebeveiliging 101:Toegangsrechten voor databases begrijpen