Not creating indexes

27 views
Skip to first unread message

Rob Polak

unread,
Dec 23, 2015, 9:48:32 AM12/23/15
to Mongoose Node.JS ODM
Mongoose: 4.2.9

I am having an issue creating indexes with mongoose.  Here is the code for my index creation:

  Call.index( { cfrom: 1}, {background: "1"});   
  Call.index( { cto: 1}, {background: "1"});

I noticed that on app startup that my collections are not being created (I thought with mongoose collections would be created on startup).

Anyways I debugged into the following mongoose Model.ensureIndexs function: and I see the following error being thrown by mongo:  no index name specified

Any thoughts?

Thanks

Rob

Richard Bateman

unread,
Dec 23, 2015, 5:31:45 PM12/23/15
to mongoo...@googlegroups.com

I don't know why this would be happening, but you could try manually specifying a name, like so:

Call.index( {cfrom: 1}, {background: true, name: 'cfrom_1'} )

Richard

--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rob Polak

unread,
Dec 29, 2015, 10:20:54 AM12/29/15
to Mongoose Node.JS ODM
This did not fix the issue, I am getting the same error.  My mongo instance is hosting on Compose.io.. could it be a mongo configuration related error?

Richard Bateman

unread,
Dec 29, 2015, 2:33:51 PM12/29/15
to mongoo...@googlegroups.com

That'd be the question; I'd try using node-inspector and stepping into mongoose.  Dig through to source to find where the actual ensureindex command is getting sent. If you can find that command then you should be able to discover what is actually being sent and find out if it's formatted correctly or not; in addition, you can try running the command yourself to see if it works.

Another option would be to look at indexes which are working and see if you can find out what the difference is. Do other indexes on that model work? If you add those fields to another model and put the index there, does it work there?

Keep fiddling, I'm sure you'll find it. I don't know of any other specific suggestions to make, though; perhaps someone else does.

Richard

Rob Polak

unread,
Jan 4, 2016, 10:50:49 AM1/4/16
to Mongoose Node.JS ODM
Right now no index's are being created at all.

I debugged into the following call:
self.collection.ensureIndex(index[0], options, tick(function (err) {
  if (err) return done(err);
  create();
}));

where index[0] is 

{
  username
:1
}


And Options are

{
   background:true,
   safe: undefined,
   unique: true
}

and I get the error:

"no index name specified"

Then I debugged into the createIndex method on the mongo client and sure enough the mongo command createCreateIndexCommand was failing to get an index name:

var createCreateIndexCommand = function(db, name, fieldOrSpec, options) {
  var indexParameters = parseIndexOptions(fieldOrSpec);
  var fieldHash = indexParameters.fieldHash;
  var keys = indexParameters.keys;

  // Generate the index name
  var indexName = typeof options.name == 'string' ? options.name : indexParameters.name;

I then tracked it down to the following method in the Utils class

else if(isObject(fieldOrSpec)

which is:

var isObject = exports.isObject = function (arg) {
  return '[object Object]' == toString.call(arg)
}


and in this case we are passing an object but "toString.call(arg)" is returning "new" instead of [object Object]

I testing a quick change and updated the code in utils.js:

var isObject = exports.isObject = function (arg) {
  return 'object' === typeof arg;
}

and like magic all of my indexes were created.

So I am guessing this might be a driver issue?

I am a little out of my realm here how to proceed.

Rob Polak

unread,
Jan 4, 2016, 10:58:56 AM1/4/16
to Mongoose Node.JS ODM
Created the following ticket : https://jira.mongodb.org/browse/NODE-633

Rob Polak

unread,
Jan 4, 2016, 12:59:20 PM1/4/16
to Mongoose Node.JS ODM
Looks like the root issue was some inproper code overwriting the global "toString" method which mongo uses. Oddly enough this code over-wrote the global toString:

campaignContactStatus.prototype.new = function() {
this.id = "new";
this.desc = "New";
this.toString = function () {
return id;
};

return this;
}();

I thought the function closure would have no impacted the global scope, I guess I was wrong.
Reply all
Reply to author
Forward
0 new messages