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

160
Vistas
Get some objects from array of objects in a document (mongoose)

I want to get some objects from array of objects in a mongodb document my document looks something like this

{
    movieDetails: {
        movieId: 333,
        movieName: "movie",
    },
    Dates: [
        {
            Date: " 01/07/2021",
            TheaterId: 12,
            Is3D: "false",
        },
        {
            Date: " 09/07/2021",
            TheaterId: 13,
            Is3D: "false",
        },
        {
            Date: " 03/07/2021",
            TheaterId: 12,
            Is3D: "false",
        }
    ]
}

I want to get only the objects where the movieId is equal to 333 and TheaterId is 12 my result should look like this

 [
        {
            Date: " 01/07/2021",
            TheaterId: 12,
            Is3D: "false",
        },
        {
            Date: " 03/07/2021",
            TheaterId: 12,
            Is3D: "false",
        }
 ]

I tried this

const dates = await Movie.find("movieDetails.movieId": 333).select({ Dates: {$elemMatch: {TheaterId: 12}}});

But it returns only the first object if anyone can help me with this I will be very grateful

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

0

According to $elemMatch docs:

The $elemMatch operator limits the contents of an field from the query results to contain only the first element matching the $elemMatch condition.

So you can use this aggregation pipeline using $filter into a $project stage like this:

Movie.aggregate([
  {
    "$match": {
      "movieDetails.movieId": 333
    }
  },
  {
    "$project": {
      "movieDetails": 1,
      "Dates": {
        "$filter": {
          "input": "$Dates",
          "as": "d",
          "cond": {
            "$eq": [
              "$$d.TheaterId",
              12
            ]
          }
        }
      }
    }
  }
])

Example here

Or this another pipeline (I prefer using $filter)

Movie.aggregate([
  {
    "$match": {
      "movieDetails.movieId": 333
    }
  },
  {
    "$unwind": "$Dates"
  },
  {
    "$match": {
      "Dates.TheaterId": 12
    }
  },
  {
    "$group": {
      "_id": "$_id",
      "movieDetails": {
        "$first": "$movieDetails"
      },
      "Dates": {
        "$push": "$Dates"
      }
    }
  }
])

Example here

about 4 years ago · Juan Pablo Isaza Denunciar

0

I would suggest taking a look at the doc for .find() since it seems you are using it incorrectly.

One way you can achieve this is to use .find() to find the movie with the ID you want, then .filter() the Dates from that object to return just the ones with your TheaterId

here is an example

const movies = [{
  movieDetails: {
    movieId: 333,
    movieName: "movie",
  },
  Dates: [{
      Date: " 01/07/2021",
      TheaterId: 12,
      Is3D: "false",
    },
    {
      Date: " 09/07/2021",
      TheaterId: 13,
      Is3D: "false",
    },
    {
      Date: " 03/07/2021",
      TheaterId: 12,
      Is3D: "false",
    }
  ]
}]

function findMovieTimesByTheater(movieID, theaterID) {
  const movie = movies.find(movie => movie.movieDetails.movieId === movieID);

  if (typeof movie === "undefined") {
    console.log(`movieID: ${movieID} not found`);
    return [];
  }

  return movie.Dates.filter(date => date.TheaterId === theaterID);
}

const times = findMovieTimesByTheater(333, 12);
console.log(times);

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