Search for text with mongo in several reference collectionsj

0

As mongodb does not support join and I have the need to search several collections of businesses, services, and users, I have arrived at a solution but I need 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,
 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:
 }]
}
})

Businesses offer services, services are carried out by specialists, and each 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 believes services assign them to specialists, these are added to the Search collection. In each modification of that document the text field will be updated with a concatenation of the name of the business, services and specialists, with this they would have a single collection where to make the indexed searches of text. Any field you look for will result in business.

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

    
asked by Antony Petrocelli 11.02.2017 в 05:46
source

2 answers

0

Basically mongodb is a non-relational database. I think what you need is to use Postgres or Mysql. Now MongoDB does not offer join (for that same reason), but you can use aggregate to generate a kind of join. My opinion is that you use Postgresql and if you want to have some quick access, make a migration to MongoDB with that data.

    
answered by 14.03.2017 в 18:55
0

The best solution to continue using mongodb (it must be mongodb), was to register the names of the services and specialists in the Business collection, create text indices combining the fields that I needed.

The only consequence of this solution is that I must keep the collection updated with the text fields belonging to documents from other collections, but it is an efficient solution, now the query is done in a single collection and not in 3.

    
answered by 17.03.2017 в 21:38