nerdexam
MongoDB

C100DBA · Question #112

You perform the following query on the sayings collection, which has the index { quote : "text" }: Assuming the documents below are in the collection, which ones will the following query return?…

Options B and C are correct. MongoDB's $text search with $search: "fact find" performs a logical OR across all words in the search string - it returns any document whose indexed text field contains at least one of the terms ("fact" or "find"). The search is also…

Performance Tuning

Question

You perform the following query on the sayings collection, which has the index { quote : "text" }:

Assuming the documents below are in the collection, which ones will the following query return? Check all that apply. db.sayings.find( { $text : { $search : "fact find" } } )

Options

  • A{ _id : 3, quote : "Nobody will ever catch me." }
  • B{ _id : 1, quote : "That's a fact, Jack." }
  • C{ _id : 2, quote : "Find out if that fact is correct." }

Explanation

Options B and C are correct.

MongoDB's $text search with $search: "fact find" performs a logical OR across all words in the search string - it returns any document whose indexed text field contains at least one of the terms ("fact" or "find"). The search is also case-insensitive, so "Find" in document C matches "find" in the query. Document B matches because it contains "fact", and Document C matches because it contains both "Find" and "fact".

Document A is incorrect because "Nobody will ever catch me." contains neither "fact" nor "find", so it matches no terms and is excluded from results.

Memory tip: Think of $text: { $search: "word1 word2" } as a Google-style keyword search - it's an OR, not an AND. If you want an exact phrase, wrap it in escaped quotes: $search: "\"fact find\"". For the exam, always ask: does this document contain any of the search words?

Topics

#text index#$text operator#text search#stemming

Community Discussion

No community discussion yet for this question.

Full C100DBA Practice