简体   繁体   中英

Query a named text index on a collection in mongo using mongoose

I'm creating a text index on my collection through mongoose with a custom name, but I'm not sure how to query that specific text index or if you can even have multiple text indexes on a single collection.

I've specified an index:

mongoose.connection.collections['jobs'].ensureIndex({ title: 'text', description: 'text' }, { name: 'customtext' });

And I can search it with the general $text query:

var query = {
  "$text": {
    "$search": "bleep bloop"
  }
}
Model.find(query, function(error, results) {});

But is there a way to add multiple different text indexes (for different searches) and query by the custom name?

A MongoDB collection can have at most one text index . You cannot define multiple text indexes for a single collection.

You can have many text indexes with different names, but the name of the index can be used primarily to remove the index.

Example:

db.collection.ensureIndex(
   { content: "text"},
   { name: "TextIndex"}
)
db.collection.dropIndex("TextIndex")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM