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

238
Vistas
How to add field with static value to mongodb find query?

Can we add some custom field with static value to mongodb find query?

I am trying to add/append API request UId to all the queries that we are making to mongodb, so that we can map requests with slow queries from mongodb logs.

I am doing it in aggregate queries by using '$literal' in projected fields. My Aggregate queries looks something like :

db.test.aggregate({
    $project: {
        "custom_id": { $literal: "uid" } 
        ..
    }
    ..
})

Also I can't include each field individually in projected fields and add the "custom_id" field with the static value.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Instead of using $comment, you can give a try to the aggregate function and the literal:

db.bids.aggregate( [
   { $project: { item: 1, startAt: { $literal: 1 } } }
] )                                                                                                           

And as a result you get:

{ "_id" : 1, "item" : "abc123", "startAt" : 1 }
{ "_id" : 2, "item" : "xyz123", "startAt" : 1 }

Doc: $literal

over 4 years ago · Santiago Trujillo Denunciar

0

Found answer to the question on mongoDb jira. The best way to do this would be using the $comment operator. Here's the link to Documentation.

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