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

santhosh
  • 1,919
  • 3
  • 21
  • 33

2 Answers2

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
        }
]
zangw
  • 43,869
  • 19
  • 177
  • 214
  • but how to search. when i do user.find({$text:{$search:"MBBS"}},function(err,doc){ if(err) cosnole.log(err) else console.log(doc) }) – santhosh Feb 16 '16 at 09:24
  • 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 Feb 16 '16 at 09:24
  • 1
    @santhosh, try `UserSchema.index({ profile: 'text' });` – zangw Feb 16 '16 at 09:28
  • @zangw , appreciate if you can have a look ..http://stackoverflow.com/questions/35426213/how-to-group-mongodb-mapreduce-output – user29578 Feb 16 '16 at 09:31
  • by doing get index from mongo shell i was able to see the creation of index – santhosh Feb 16 '16 at 09:34
  • @santhosh, may drop the previous index through `dropIndexes`, then recreate `text index` through `{profile: 'text'}`... – zangw Feb 16 '16 at 09:41
  • i'm again getting the same error:> error: need exactly one text index for $text query – santhosh Feb 16 '16 at 09:49
  • @santhosh, I got it, please remove the ` index: true` from `profile`, just, `profile : {type: String},` I think it could solve your issue – zangw Feb 16 '16 at 09:56
  • @zangw this time getindexes :{ "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.user" } – santhosh Feb 16 '16 at 10:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103587/discussion-between-zangw-and-santhosh). – zangw Feb 16 '16 at 10:10
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.

oleh.meleshko
  • 4,675
  • 2
  • 27
  • 40