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

206
Vistas
searching for a phrase in Mongodb: weird behaviour

i have the following collection:

books> db.books.find()
[
  {
    _id: ObjectId("61ab85b0056b5357b5e23e6b"),
    fields: 'hello good morning'
  },
  { _id: ObjectId("61ab85b5056b5357b5e23e6c"), fields: 'good morning' },
  {
    _id: ObjectId("61ab8679056b5357b5e23e6d"),
    fields: 'hello good morning guys'
  },
  {
    _id: ObjectId("61ab8684056b5357b5e23e6e"),
    fields: 'good morning guys'
  }
]

Then, i run this query:

db.books.find({$text : {$search : "\"good morning\" hello"}})

and i get:

[
  { _id: ObjectId("61ab85b5056b5357b5e23e6c"), fields: 'good morning' },
  {
    _id: ObjectId("61ab8684056b5357b5e23e6e"),
    fields: 'good morning guys'
  },
  {
    _id: ObjectId("61ab85b0056b5357b5e23e6b"),
    fields: 'hello good morning'
  },
  {
    _id: ObjectId("61ab8679056b5357b5e23e6d"),
    fields: 'hello good morning guys'
  }
]

Can you help me to understand the output? The first result doesn't make much sense to me (document _id : ...23e6c), as it doesn't contain the "hello" string.

I read the first answer to this question. Does it mean that, in my case, mongodb is searching for

(good morning) AND (good OR morning OR hello)

It would explain my question, but i can't find the exact reference to this in the mongodb documentation

Thanks in advance

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

0

Directly from the docs:

A string of terms that MongoDB parses and uses to query the text index. MongoDB performs a logical OR search of the terms unless specified as a phrase. See Behavior for more information on the field.

This is just the behavior a text index allows, What you can do it mark every single expression as a "phrase", like so:

{$text : {$search : "\"good morning\" \"hello\""}}

This will enforce $and logic.

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