MongoDB: $or a full-text search and an $in

别来无恙 提交于 2021-02-18 10:42:12

问题


The problem

Hi. I have what seems to me as a strange problem and I'm at a loss with it:

Let's take:

tags   = [ ObjectId('a'), ObjectId('b') ]
search = { $search: 'abc' }

Now the following query works fine:

db.entries.find({ $or: [ {$text:search} ] })

And this one too:

db.entries.find({ $or: [ {tags:{$in:tags}} ] })

But combine them:

db.entries.find({ $or: [ {$text:search}, {tags:{$in:tags}} ] })

And I get the following error:

Unable to execute query: error processing query: ns=db.entries
Tree:
  $or
    tags $in [ ObjectId('a'), ObjectId('b') ]
    TEXT : query=abc, language=, tag=NULL
Sort: {}
Proj: {}
 No query solutions

Meta-data

  • I'm using MongoDB version 2.6.4.
  • Combining either of the conditions with a simple {_id:"c"} expression works fine.
  • I do have my text-indices set up properly.
  • The order in which the conditions appear in the $or-array doesn't influence the outcome.

My question

Help? :(


回答1:


Running the query under a slightly different environment produced a much more clear error:

Runner error: BadValue error processing query: ns=webistor.entries limit=0 skip=0
Tree: $or
    tags $in [ ObjectId('a') ObjectId('b') ]
    TEXT : query=abc, language=, tag=NULL
  Sort: {}
  Proj: {}

planner returned error: Failed to produce a solution for TEXT under OR - other non-TEXT clauses under OR have to be indexed as well.

Note

Other non-TEXT clauses under OR have to be indexed as well

Apparently I'll have to add an index to tags.



来源:https://stackoverflow.com/questions/26116182/mongodb-or-a-full-text-search-and-an-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!