Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda