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

165
Visualizações
Querying inside a subdocument array in mongodb

Am really new to MongoDB or NoSQL database. I have this userSchema schema

const postSchema = {
    title: String,
    posted_on: Date
}

const userSchema = {
    name: String,
    posts: [postSchema]
}

I want to retrieve the posts by a user in given range(/api/users/:userId/posts?from=date&to=date&limit=limit) using mongodb query. In a relational database, we generally create two different sets of tables and query the second table(posts) using some condition and get the required result. How can we achieve the same in mongodb? I have tried using $elemMatch by referring this but it doesn't seem to work.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

2 ways to do it with aggregation framework, that can do much more than a find can do.

With find we mostly select documents from a collection, or project to keep some fields from a document that is selected, but here you need only some members of an array, so aggregation is used.

Local way (solution at document level) no unwind etc

Test code here

Query

  • filter the array and keep only posted_on >1 and <4 (i used numbers fro simplicity use dates its the same)
  • take the first 2 elements of the array (limit 2)
db.collection.aggregate([
  {
    "$match": {
      "name": {
        "$eq": "n1"
      }
    }
  },
  {
    "$set": {
      "posts": {
        "$slice": [
          {
            "$filter": {
              "input": "$posts",
              "cond": {
                "$and": [
                  {
                    "$gt": [
                      "$$this.posted_on",
                      1
                    ]
                  },
                  {
                    "$lt": [
                      "$$this.posted_on",
                      5
                    ]
                  }
                ]
              }
            }
          },
          2
        ]
      }
    }
  }
])

Uwind solution (solution at collection level) (its smaller a bit, but keeping things local is better, but in your case it doesn't matter)

Test code here

Query

  • match user
  • unwind the array, and make each member to be ROOT
  • match the dates >1 <4
  • limit 2
db.collection.aggregate([
  {
    "$match": {
      "name": {
        "$eq": "n1"
      }
    }
  },
  {
    "$unwind": {
      "path": "$posts"
    }
  },
  {
    "$replaceRoot": {
      "newRoot": "$posts"
    }
  },
  {
    "$match": {
      "$and": [
        {
          "posted_on": {
            "$gt": 1
          }
        },
        {
          "posted_on": {
            "$lt": 5
          }
        }
      ]
    }
  },
  {
    "$limit": 2
  }
])
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