mongodb/mongoid · error
Multiple $text expressions per query are not currently…
Error message
Multiple $text expressions per query are not currently supported by the server
What it means
Raised by Selectable#text_search when the query already contains a $text expression and another text_search call is made. MongoDB allows at most one $text search per query, so a second expression cannot be merged; the faulting input is the pre-existing $text key in the selector.
Solutions
- Combine the search terms into a single text_search call
- Start a new criteria (e.g. Model.all.text_search(...)) instead of stacking text_search twice
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at lib/mongoid/criteria/queryable/selectable.rb:743 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of mongodb/mongoid@0da0e23d71 (2026-08-23).
Data as JSON: /api/errors/1fc2ac0f35bc3cfd.
Report an issue: GitHub.
Appendix: source
Thrown at lib/mongoid/criteria/queryable/selectable.rb:743
# @param [ String | Symbol ] terms A string of terms that MongoDB parses
# and uses to query the text index.
# @param [ Hash ] opts Text search options. See MongoDB documentation
# for options.
#
# @return [ Selectable ] The cloned selectable.
def text_search(terms, opts = nil)
raise Errors::CriteriaArgumentRequired, :terms if terms.nil?
clone.tap do |query|
criterion = { '$text' => { '$search' => terms } }
criterion['$text'].merge!(opts) if opts
if query.selector['$text']
# Per https://www.mongodb.com/docs/manual/reference/operator/query/text/
# multiple $text expressions are not currently supported by
# MongoDB server, but build the query correctly instead of
# overwriting previous text search condition with the currently
# given one.
Mongoid.logger.warn('Multiple $text expressions per query are not currently supported by the server')
query.selector = { '$and' => [ query.selector ] }.merge(criterion)
else
query.selector = query.selector.merge(criterion)
end
end
end
# This is the general entry point for most MongoDB queries. This either
# creates a standard field: value selection, and expanded selection with
# the use of hash methods, or a $where selection if a string is provided.
#
# @example Add a standard selection.
# selectable.where(name: "syd")
#
# @example Add a javascript selection.
# selectable.where("this.name == 'syd'")
#
# @param [ [ Hash | String ]... ] *criterion The standard selectionView on GitHub (pinned to 0da0e23d71)