How to store URL value in Mongoose Schema?

Source picture Source · Nov 6, 2017 · Viewed 7.6k times · Source

I am uploading the images from an IOS application to Firebase which returns to me the metadata including the URL of type URL.

Should I store it of type String in the database like below code? or there is specific type for URLs?

var schema = new Schema({
  downloadURL: { type: String, createdDate: Date.now }
})

Answer

Mahmoud Ibrahim picture Mahmoud Ibrahim · Nov 6, 2017

Well, As per the docs Monngoose doesn't have a schema type for URL. You could just use a string with RegExp to validate it or use some custome made type like this one

var mongoose = require('mongoose');
require('mongoose-type-url');

var UserSchema = new mongoose.Schema({
url: {
    work: mongoose.SchemaTypes.Url,
    profile: mongoose.SchemaTypes.Url
}
});