1

As mongodb does not support join and I have the need to search in various business collections, services, and users, I have come up with a solution but I need it to be validated and / or improved.

The flow of the scheme would look like this:

Business = Schema({
  name:String
  services:[{
    type:ObjectId,
    ref:'Services'
  }],
  specialist:[{
    type:ObjectId,
    ref:'User'
   }]
})

User = Schema({
  full_name:String
  businesses:[{
    _id:{
       type:ObjectId,
       ref:'Business'
    },
    name:String,
    role:String,
    is_owner:Boolean
  }]
})

Service = Schema({
  name:String,
  business:{
    type:ObjectId,
    ref:'Business'
  },
  specialist:[{
    type:ObjectId,
    ref:'User'
  }]
})

Search = Schema({
  text:{
    type:String,
    index:'text'
  },
  business:{
    _id:{
      type:ObjectId,
      ref:'Business'
     },
    name:String
 },
 services:[{
  _id:{
    type:ObjectId,
    ref:'Service'
  },
  name:
 }],
 specialist:[{
  _id:{
    type:ObjectId,
    ref:'User'
  },
  full_name:String
 }]
})

Businesses offer services, services are performed by specialists, and every time a business adds a specialist, the user will have a new business in their data with a specific role.

The idea is that when you create a business, you create a document in Search, and when the business creates services you assign them to the specialists, they will be added to the Search collection. In each modification of this document the text field will be updated with a concatenation of the business name, services and specialists, with this they would have a single collection where to do the searches indexed of text. Any field you are looking for will result in business.

Who has suggestions, both in model and in the solution of the common collection to search.

1 Answers1

1

Check out $lookup (aggregation) new in version 3.2.

Performs a left outer join to an unsharded collection in the same database to filter in documents from the “joined” collection for processing. The $lookup stage does an equality match between a field from the input documents with a field from the documents of the “joined” collection.

By the way,the advantage of a document database is that it eliminates lots of Joins. Related information is stored together for fast query access through the MongoDB query language. This data model give you the ability to represent hierarchical relationships, to store arrays, and other more complex structures easily. So I suggest when you design a MongoDB schema you should place as much in a single document as you can.

You can see more details from MongoDB relationships: embed or reference?.

Hope this helps.

Community
  • 1
  • 1
McGrady
  • 10,869
  • 13
  • 47
  • 69