Full text search with weight in mongoose

前端 未结 4 1566
感动是毒
感动是毒 2020-12-07 10:35

As I find out, since version 3.8.9, mongoose support full text search. But I can\'t find a good documentation for it!
I want to do something like:

db.col         


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-07 11:05

    I found the following article that led me to the http://code.tutsplus.com/tutorials/full-text-search-in-mongodb--cms-24835 I dropped the index created in the top answer using the following

    db.tablename.dropIndex({"indexname_text"})  
    

    I got the list of indexes with this command

    db.tablename.getIndexes()
    

    I then used the following to create the indexes

    db.tablename.createIndex({"$**":"text"})
    

    the following commands worked in Mongoose

    model.find(
        {$text: {$search: "text you are searching for"}},
        {score: {$meta: "textScore"}})
        .sort({score:{$meta:"textScore"}}
    )
    .exec(function(err, results) {
        `enter code here`if(!err){
        console.log('results ' + results);
    }
    else
    {
        console.log(err);
    }
    });
    

提交回复
热议问题