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

475
Vistas
Combine $in with $split in mongo

I want to combine $in with $split like in the following example, but it fails saying "$in needs an array". I understand the output of $split is an array so I don't know why it fails. Do you know how to solve it or another way to do it? Thanks

db.mydoc.aggregate([
    {
        '$match': {
            'myid': {
                '$in': {
                    '$split': [
                        '136618,136620,136622',
                        ',',
                    ],
                },
            },
        },
    },
    {
        '$project': { ... },
    },
]);
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

This "fail" is the expected behavior, let's understand why.

We must first take a look at the $match behavior as specified in the docs:

$match takes a document that specifies the query conditions. The query syntax is identical to the read operation query syntax; i.e. $match does not accept raw aggregation expressions. Instead, use a $expr query expression to include aggregation expression in $match.

This means when you use $match it uses the query language by default, now the "issue" comes from the difference between the two $in operators the query $in operator (which is being used) and the aggregation $in operator ( which you assume is being used ).

It is true that $split resolves to an array. but $split is also an aggregation operator, now I think this case should throw an error but for some reason as you mentioned this behavior just resolves with no results. the aggregation $in operator however , does accept raw aggregation expressions.

This means all you have to do is convert your $match query to use $expr so you can use the aggregation version of $in within the match, like so:

db.collection.aggregate([
  {
    "$match": {
      $expr: {
        $in: [
          "$myid",
          {
            "$split": [
              "136618,136620,136622",
              ","
            ]
          }
        ]
      }
    }
  }
])

Mongo Playground

over 4 years ago · Santiago Trujillo Denunciar

0

@Tom Slabbaert gave a very comprehensive and good answer. Just for sake of completeness, an alternative solution (if you work with Javascript/Mongo shell) is this one:

db.mydoc.aggregate([
    {
        '$match': {
            'myid': { '$in': '136618,136620,136622'.split(',') }
        }
    },
    {
        '$project': { ... },
    },
]);

Be aware either solutions create an array of strings, i.e. [ "136618", "136620", "136622" ]. It does not match if your collection has numeric values, e.g. { myid: 136618 }

You may use

'136618,136620,136622'.split(',').map(x => NumberInt(x))

or

{ $map: { input: { "$split": ["136618,136620,136622", ","] }, in: { $toInt: "$$this" } } }
over 4 years ago · Santiago Trujillo 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