Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
Mongo return data based on nested date params

I'm scraping and saving a bunch of data into a mongodb collection by pushing it into an array, an example of the saved data looks like this:

{ 
    "_id" : ObjectId("616eafce1e7edf6e9b033938"), 
    "collectibleId" : "34e05c2c-19dd-4509-a98c-bb11cd275d34", 
    "__v" : NumberInt(0), 
    "createdAt" : ISODate("2021-10-19T11:45:16.845+0000"), 
    "history" : [
        {
            "storePrice" : 6.99, 
            "value" : NumberInt(27), 
            "date" : ISODate("2021-10-19T11:45:16.683+0000"), 
            "totalListings" : NumberInt(904), 
            "hourlyChange" : {
                "market" : NumberInt(0)
            }
        }, 
        {
            "storePrice" : 6.99, 
            "value" : 27.5, 
            "date" : ISODate("2021-10-19T11:59:01.192+0000"), 
            "totalListings" : NumberInt(902), 
            "hourlyChange" : {
                "market" : NumberInt(0)
            }
        }, 
        ... loads more items ...
   ],
    "updatedAt" : ISODate("2021-10-27T18:59:01.109+0000")
}

I'm trying to now write a query for the data that can return only the recorded data within a specific timeframe. So far I've tried this:

Prices.findOne({
        collectibleId: slug,
        history: {
            date: {
                $gte: new Date("2021-10-26T00:00:00.000Z")
            }
        }
    }).exec((err, data) => {
        if (err){
            return res.status(400).json({
                error: errorHandler(err)
            })
        }
        res.json(data)
    })

But the result im getting is always null. I'm wondering if it's because date is nested in history and perhaps im writing that wrong ? Any advice appreciated thank you.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

$match then $set

db.collection.aggregate([
  {
    "$match": {
      collectibleId: "slug",
      "history.date": {
        $gte: ISODate("2021-10-26T00:00:00.000Z")
      }
    }
  },
  {
    "$set": {
      "history": {
        "$filter": {
          "input": "$history",
          "as": "h",
          "cond": {
            $gte: [
              "$$h.date",
              ISODate("2021-10-26T00:00:00.000Z")
            ]
          }
        }
      }
    }
  }
])

mongoplayground

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda