There is a collection, you need to do a search in it. To do this, you need to create an index, which will then be used to search. The problem is that through the console I can create it:

db.films.createIndex({"title":"text", "stars":"text"}) 

But in express'e I just can not. Here is the server code:

 router.post('/find', function(req, res, next) { Film.createIndex({ "title": "text" }); Film.find({ $text: { $search: "war" } }, { score: { $meta: "textScore" } }, function(err, docs) { if (err) { console.log(err) } console.log(docs) }) }); 

When I run this code, I get an error 500 into the console . Tell me, where did I make a mistake?

I already figured out the error, "index is not a function". But now another problem, I can not create the required index through the schema (model). Tried so

 var filmSchema = new Schema( { title: { type: String, index: true }, releaseYear: Number, format: String, stars : { type: String, index: true } }); 

But with this way, at the output I get the wrong index that I need (i.e., the way I create manually via the command line)

  • in most cases, there is no point in creating indexes in the HTTP request handler. Indexes need to be created when designing a database and with, conditionally, support. so the only correct solution to this situation would be to remove the creation of the index from the router. and the 500th response status indicates, most likely, that such an index already exists. - nörbörnën
  • @norbornen I already found an error, when creating an index in a router, the interpreter says that index is not a function. And I suspected that the indices should be set in the scheme itself, but what I set in the scheme does not turn out exactly the way I need it. This is what I specify in the title scheme: {type: String, index: true}, but as a result I get a different index than I would like - Vladislav
  • you need to say filmSchema.index({title: 1, start: 1}) , if I remember correctly - nörbörnën

2 answers 2

The problem is that you specify in your schema that for two fields you need to create an index. While you need one composite index. The mongoose documentation describes how to do this. In your case it turns out

 var filmSchema = new Schema( { title: String, releaseYear: Number, format: String, stars: String }); filmSchema.index({ title: 1, stars: 1 }); 

And I want to draw attention to the fact that it is recommended (ibid., In the documentation) to disable the auto- config.autoIndex = false indexes in the production system by setting config.autoIndex = false .

upd: Although it is believed that it is not so important. More information here in this answer.

    Thank you @Ovsyanka and @norbornen for your answers. They just reflect the essence of what ede needs to create composite indexes.

    In my case, for searching in two fields, the following option is available, can someone else come in handy:

     var filmSchema = new Schema( { title: String, releaseYear: Number, format: String, stars: String }); // создание конструктора filmSchema.index({ title: "text", stars: "text" });