Consider I have multiple collections users
, posts
, comments
, groups
, I want to refer to all these different types, using a json array containing object Ids and reference.
ref_ids: [{
objectId: {
type: Schema.Types.ObjectId,
required:true
},
ref: {
type: String,
enum: ['users', 'posts', 'comments', 'groups'],
required: true
}
}]
What could be the possible disadvantages of using such a schema?
How would you approach querying this using $lookUp
, it seems possible?
Expected Output should be like below -
[
{
'ref_ids': [
{
'object_id': 'hash',
'ref': 'user',
'refObject': {
'_id': 'hash',
'name': 'Piyush'
}
},
{
'object_id': 'hash',
'ref': 'post',
'refObject': {
'_id': 'hash',
'text': 'We might look this up'
}
}
]
},
{
'ref_ids': [
{
'object_id': 'hash',
'ref': 'post',
'refObject': {
'_id': 'hash',
'name': 'We might look this up'
}
},
{
'object_id': 'hash',
'ref': 'comment',
'refObject': {
'_id': 'hash',
'text': 'We won\'tlookthisup'
}
}
]
}
]