Creating an aggregation query possible ?

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'
        }
      }
    ]
  }
]

Hi @crossdsection,

Welcome to MongoDB community.

It seems that the idea behind your schema is more oriented on a relational schema design where data is normalised and referenced.

This design looses the advantages of MongoDB to embeed data within a main document to query it without the need of doing joins aka lookups.

Lookups complex code and cause performance overhead.

Thinking about a blog like schema I don’t think that comments have justification to be on their own without a post for example.

I would think that a post document should embed comments , each one can be a complex object with the commentor details:

{ post_id,
  auther_id,
   post_text,
 comments: [ { user_id, user_name, comment}, ....]

The user documents can have ids of recent posts.

I think that groups should exist in users objects like “tags” as a group is probably only collection of users labled with this group details. Group id can be indexed for faster user find within a group:

userid,
username,
groups : [ { groupid , groupinfo}]

I recommend reading following blogs:
https://developer.mongodb.com/article/mongodb-schema-design-best-practices/

https://developer.mongodb.com/article/schema-design-anti-pattern-summary

Thanks
Pavel

2 Likes