owais
owais

Reputation: 4922

change _id string to base64 encoded string in mongodb via mongoose

By default mongodb generates some random string like 5548087025c7f79259525eff its type is ObjectId. Instead of that random string i want to use base64 encoded string so that it will always start from alphabet because I want CRUD on embeded documents which is not possible if id starts from integer according to my understanding. also store as string instead of ObjectId. Better if solution is using mongoose.

I can encode string to base64 using nodejs as in my case. but i dont know where to change this default behavior ... thanks a lot

Upvotes: 0

Views: 1866

Answers (1)

Brian Shamblen
Brian Shamblen

Reputation: 4703

Just define the _id property in your schema and set the type option as String. You can either set a default value as a function to generate the value, or manually set it when you create the document.

    _id: { type: String, required: true, index: true, unique: true }

Upvotes: 1

Related Questions