4

I was not able to create index for profile:

var user = new Schema({
      profile : "String",
      fullname:"String"
   })
    user.statics.createIndexOfProfile = function(callback){
    this.ensureIndex("profile",function(err,doc){
        if(err) {
          console.log(err+'sna');
          callback(err);
        }
         else {
          console.log(doc+'santhosh');
          callback(null,doc);
        }
      });

I was getting error like this.ensureIndex is not a function

0

2 Answers 2

7

The correct API is ensureIndexes, which Sends ensureIndex commands to mongo for each index declared in the schema.

Here is one sample

var UserSchema = new Schema({
    profile : {type: String, index: true},
    fullname: String
});

var User = mongoose.model('User', UserSchema);

User.ensureIndexes(function(err) {
    if (err)
        console.log(err);
    else
        console.log('create profile index successfully');
});

Or through index

var UserSchema = new Schema({
    profile : {type: String, index: true},
    fullname: String
});

UserSchema.index({ profile: 1 });

var User = mongoose.model('User', UserSchema);

After running the above codes, then check the indexes from MongoDB.

> db.users.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "test.users"
        },
        {
                "v" : 1,
                "key" : {
                        "profile" : 1
                },
                "name" : "profile_1",
                "ns" : "test.users",
                "background" : true
        }
]
Sign up to request clarification or add additional context in comments.

10 Comments

but how to search. when i do user.find({$text:{$search:"MBBS"}},function(err,doc){ if(err) cosnole.log(err) else console.log(doc) })
errror is like "$err": "Unable to execute query: error processing query: ns=user.profile limit=1000 skip=0\nTree: TEXT : query=santhosh, language=, tag=NULL\nSort: {}\nProj: {}\n planner returned error: need exactly one text index for $text query",
@santhosh, try UserSchema.index({ profile: 'text' });
@zangw , appreciate if you can have a look ..stackoverflow.com/questions/35426213/…
by doing get index from mongo shell i was able to see the creation of index
|
1

If you want to add an index to some field, just add index: true to its definition.

So you can do the following:

var user = new Schema({
    profile : { type: String, index: true }, // field level
    // ...
});

or

user.index({ profile: 1 }); // schema level

From mongoose docs:

When your application starts up, Mongoose automatically calls ensureIndex for each defined index in your schema. Mongoose will call ensureIndex for each index sequentially, and emit an 'index' event on the model when all the ensureIndex calls succeeded or when there was an error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.