E
E
Egor2016-05-29 14:17:15
MongoDB
Egor, 2016-05-29 14:17:15

How to make a unique slug for a page?

I'm trying to make an application on node.js. I was puzzled by the creation of beautiful url.
Now they look like this
localhost/item/574aac2fc4621ec02f12c301
I would like to do something like this
localhost/item/pervia-page
That is, now the id from mongodb is being substituted.
When added to the database, json arrives with a certain number of elements, and it can contain elements with the same name, and the database can also have such names, but everyone has different characteristics. I think you need to add a number to the end of the same slugs so that they differ. How to check that there are no identical slugs?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoly, 2016-05-29
@taliban

Generate a slug based on some criterion (title or something else, it doesn't matter) and see if it is in the database.
Your K.O.

L
lega, 2016-05-29
@lega

How to check that there are no identical slugs?
Make a unique index on this field and there will be no duplicates.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question