Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
976 views
in Technique[技术] by (71.8m points)

node.js - How to do multiple text search using "$text query and $or" in mongodb / mongoose?

Here is the model 'Class' model for which I have created the "text" index for 'keywords','lifeArea',''type'.

Structure of the model:

{
    "_id" : ObjectId("558cf6e3387419850d892712"),
    "keywords" : "rama,seetha",
    "lifeArea" : [
        "Emotional Wellness"
    ],
    "type" : "Pre Recorded Class",
    "description" : "ram description",
    "synopsis" : "ram syn",
    "name" : "ram demo",
    "__v" : 0
}

db.Class.getIndexes() 
// displaying index
{
        "v" : 1,
        "key" : {
            "_fts" : "text",
            "_ftsx" : 1
        },
        "name" : "classIndex",
        "ns" : "innrme.classes",
        "weights" : {
            "keywords" : 1,
            "lifeArea" : 1,
            "type" : 1
        },
        "default_language" : "english",
        "language_override" : "language",
        "textIndexVersion" : 2
}

I want to do a text search on the fields mentioned above. I tried the following query.

db.classes.find({$or:[{keywords: { $text: { $search: "rama abc" } } }, {type: {$text: { $search: "class" }}}],score: {$meta: 'textScore'}});

But it did not work and I got the follwing error

Error: error: {
    "$err" : "Can't canonicalize query: BadValue unknown operator: $text",
    "code" : 17287
}

Please help me to get the correct query. Please correct/educate me if I am wrong in asking the question or in explaining the problem

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all I don't think you can use $text in that manner, you need first to create a text index on the collection then you can use it but without specifying any field because it works on indexes not fields.

Please check here: http://docs.mongodb.org/manual/administration/indexes-text/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...