Search code examples
mongodbfull-text-indexing

$and operator on multiple $text search in mongo


Is it possible to have $and operator on multiple $text index search in mongo?

I have documents in tp collection of my db

> db.tp.find()
{ "_id" : ObjectId("...."), "name" : "tp", "dict" : { "item1" : "random", "item2" : "some" } }
{ "_id" : ObjectId("...."), "name" : "tp", "dict" : { "item3" : "rom", "item4" : "tttt" } }

Then I do

> db.tp.createIndex({ "$**": "text" })
> db.tp.find({ $and: [{$text : { $search: "random" } }, {$text : { $search: "redruth" } }]})

And it fails with

Error: error: {
"waitedMS" : NumberLong(0),
"ok" : 0,
"errmsg" : "Too many text expressions",
"code" : 2
}

but text index search works for single search so is it not possible to bind multiple text searches with $and operator? By the way I am using wildcard character $** for indexing because I want to search over entire document.


Solution

  • A query can specify at most one $text expression. See:

    https://docs.mongodb.com/manual/reference/operator/query/text/