I have no idea if this is even possible but I'd like to find out if there's some way to create a relationship between a document and a whole collection. I have a user schema that looks something like this:
var userSchema = new Schema({
fname: {type: String, required: true},
lname: {type: String, required: true},
email: {type: String, required: true, unique: true},
password: {type:String, required: true}
});
To achieve my desired goal, I'm thinking my schema would need to look a bit like this:
var userSchema = new Schema({
fname: {type: String, required: true},
lname: {type: String, required: true},
email: {type: String, required: true, unique: true},
password: {type:String, required: true},
collections: [{type: Schema.Types.ObjectId}] // where ObjectId would be the ID to a collection
});
The problem with this approach is, I have no way to get the ID of a collection. I'm unsure if that's even a thing. Any suggestions? Is this even possible?
Any help is appreciated.
Best Answer
Collections do not have IDs. I would suggest you just use the collection names. You can already dynamically access collections by name - so enumerating stored names in this collections array to access the actual DB collections would be no problem.