I'm trying to use the fulltext search. Setting the index in this way
myRootSchema.index({ "_type": 1, "$**": "text" });
where _type is a discriminatorKey and myRootSchema is the father schema of 4 inherited schemas.
I get this error
{
"name": "MongoError",
"message": "error processing query: ns=MYDB.caseNotesTree: TEXT : query=title, language=english, caseSensitive=0, diacriticSensitive=0, tag=NULL\nSort: {}\nProj: {}\n planner returned error: failed to use text index to satisfy $text query (if text index is compound, are equality predicates given for all prefix fields?)",
"waitedMS": 0,
"ok": 0,
"errmsg": "error processing query: ns=MYDB.caseNotesTree: TEXT : query=title, language=english, caseSensitive=0, diacriticSensitive=0, tag=NULL\nSort: {}\nProj: {}\n planner returned error: failed to use text index to satisfy $text query (if text index is compound, are equality predicates given for all prefix fields?)",
"code": 2
}
Trying this query
Model
.find(
{ $text : { $search :req.query.q } }
)
.exec(function(err, data) {
if(err)res.json(err)
res.json(data)
});
EDIT: as suggested, I should set the _type field in the query, but the _type is 'autofilled' since is a discriminator. A query with a single _type works, but I don't need that, I have to query 4 inherited models. I even tried an $or, doesn't work with the same error.
Model
.find(
{ $or: [ { _type: 'childA' },
{ _type: 'childB' },
{ _type: 'childC' },
{ _type: 'childD' }
], $text : { $search :req.query.q } }
)
.exec(function(err, data) {
if(err)console.log(err)
res.json(data)
});
Modelyour root model or the model for one of your 4 inherited schemas?_typeis only going to be added to your queries for the inherited models. Instead of that$or, have you tried adding_type: {$in: ['childA', 'childB', 'childC', 'childD']}?_typevalues while also using a$textquery, which makes sense given how the compound index is constructed. You'll need to$textquery each_typeseparately and then merge the results together; effectively creating your own$orquery.