Wednesday, 3 May 2017

Increment an index based on field in Mongoose

Im working on a project using MEAN stack. I have a Schema in mongoose where I need to create and increment an index, to do that I declare this line of code in my model:

PageSchema.plugin(AutoIncrement, {inc_field: 'index'});

The problem is that in my schema I have a field that encloses my pages in modules so if I have:

PageSchema: {
  _id: 12345678,
  moduleId: 123,
  etc...
}

I want to increment the index of every page that is created for module 123, but I want those indexes to be exclusive from the indexes of the pages of, say, module 456. Is there a way to implement that from a simple or simpleish line of code like the one I use to Autoincrement?



via Edy Luro

No comments:

Post a Comment