talha-asad / mongoose-url-slugs

Create URL compatiable slugs on mongoose models, ensuring uniqueness.
MIT License
40 stars 22 forks source link

Create Slug for Sub documents? #13

Closed viztastic closed 8 years ago

viztastic commented 8 years ago

Hi Talha,

Thanks for this module, it's great. I have a particular use case in mind:

I have a document (e.g. "Book") I currently use mongoose-url-slugs to create a slug for it. But now I have sub documents (e.g. Chapters within the book) that I'd also like to create a slug for each individual chapter.

Example Schema

{ 
  "bookName" : "Bears go Shopping",
  "slug" : "bears-go-shopping",
  "chapters": 
               [{
                "chapterName":"Authors Intro",
                "slug":"authors-intro"
                     },{
                "chapterName":"A beautiful day",
                "slug":"a-beautiful-day"
               }]
}

Is there a way to achieve this?

viztastic commented 8 years ago

I should note that I have tried doing that to date, I have a "BookSchema" and a "chapterSchema" htat look something like this


var urlSlug = require('mongoose-url-slugs');

var bookSchema = new Schema({
     "bookName" : {type:String},
     "chapters": [chapterSchema]
})

var chapterSchema = new Schema({
    "chapterName" : {type:String}

})

bookSchema.plugin(urlSlug("bookName"));
chapterSchema.plugin(urlSlug("chapterName"));

This seems to return an error The example is extremely simplified, but hopefully it conveys what I am trying to do.

I have also tried playing with settings like chapterSchema.plugin(urlSlug("chapterName",{index:false,index_required:false}));

{ [MongoError: E11000 duplicate key error index: salis-test.contents.$contents.slug_1 dup key: { : null }] name: 'MongoError', message: 'E11000 duplicate key error index: salis-test.contents.$contents.slug_1 dup key: { : null }', driver: true, code: 11000, index: 0, errmsg: 'E11000 duplicate key error index: salis-test.contents.$contents.slug_1 dup key: { : null }', getOperation: [Function], toJSON: [Function], toString: [Function] }

talha-asad commented 8 years ago

Please empty the collections first and then try again. The error message generally pops up if you already have documents that don't obey the index definitions.

viztastic commented 8 years ago

Hi @talha-asad , thanks for the response, I did the following steps:

$ mongo [DatabaseName]
$ db.[collectionName].remove({})
$ db.[collectionName].drop()

Then I re-ran the node app and I got this error instead:

image

talha-asad commented 8 years ago

What version of mongoose, mongodb and node are you using. I'll try and reproduce this error.

viztastic commented 8 years ago

HI @talha-asad

Sorry for the belated response:

Mongoose Version: 4.3.4 NodeJS version: 4.2.1 MongoDB version: 3.0.5

Much appreciated!