so I'd like to know if there is a way to add new fields to a mongoose schema dynamically. Or to be more precise, I want to add a new ObjectId with a reference and a autopopulation object afterwords.
Currently, my schema looks like this:
var Reports = new Schema({
reportedBy: {
type: Schema.Types.ObjectId,
ref: 'Accounts',
autopopulate:{
select:'username'
}
},
comdata: {
type: Schema.Types.ObjectId,
ref: 'Comments',
autopopulate: {
select: 'by content'
}
},
usrdata: {
type: Schema.Types.ObjectId,
ref: 'Accounts',
autopopulate: {
select: 'name dob mail created ip'
}.....
So I have a collection Accounts
for all account details, a Comments
collection for user comments and also a Reports
collection for all reported users AND comments. Currently, I use two fields (comdata and usrdata) to autopopulate the details from the other databases so that I'm able to get the details of reported items. But it's not only looking very bad, it's also a inefficient. I have to use switches to be able to get the correct data and some other workarounds to work with the data.
However, I wanted to know if it is possible to remove the comdata and usrdata from the declared schema (including the autopopulate field) and add it to the documents dynamically, so I don't have to use two or more different fields.
I had a look at the mongoose query documentation about dynamic references, but I don't quite know if and how that works. Thanks a lot for helping!
via Blade
No comments:
Post a Comment