Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!