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

113
Vistas
Mongoose query user ids inside an array property

I have a messaging app where a conversation model stores the participants of a unique conversation.

I am trying to query for a specific conversation where two userIds are inside the participant array but I get the error message 'Query filter must be an object, got an array '.

I tried putting the array inside an object but it didn't work and not sure how to make this query work OR if there is a better way to perform this search.

What am I doing wrong?

MODEL

const conversationSchema = mongoose.Schema(
  {
    participants: [
      {
        type: mongoose.Schema.Types.ObjectId,
        ref: `User`
      }
    ]
  }
)

EXPRESS ROUTE CODE


  const existingConvo = await Conversation.findOne({
        participants: { $elemMatch: [msg.senderId, msg.recId] },
        // ^ Query filter must be an object, got an array 
      })

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

0

Your use case should require a $all instead of $elemMatch since you require the participants array to contain both/all users in your query array.

Your code should be like this:

const existingConvo = await Conversation.findOne({
    participants: { $all: [msg.senderId, msg.recId] }
})

Here is the Mongo playground for you to reference the behaviour of $all in native mongo query.

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