Wildcard Text Index and inherited schemas

馋奶兔 提交于 2019-12-11 12:25:11

问题


I have 1 root schema and 4 inherited schemas using a discriminatorKey http://mongoosejs.com/docs/discriminators.html

In this scenario I'm not clear if I can use a wild card text index. The purpose is to index all the string fields of each inherited schema independently. https://docs.mongodb.org/manual/core/index-text/

EDIT Adding this myRootSchema.index({ "_type": 1, "$**": "text" }); on my mongoose root schema I get this error when I try to query. Should I repeat this on the inherited schemas too?

{
"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
}

the query:

Model
    .find(
        { $text : { $search :req.query.q } }

    )
    .exec(function(err, data) {
        if(err)res.json(err)
        res.json(data)
    });

回答1:


To efficiently utilize a wildcard index with inherited schemas, you'd want to use a compound text index using the kind discriminator field as the leading field in the index:

{ kind: 1, "$**": "text" }


来源:https://stackoverflow.com/questions/35896538/wildcard-text-index-and-inherited-schemas

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