Mongodb get records with priority

0

I have 3 collections in MongoDB being two lists containing only a unique identifier and in the collection remaining the unique identifier, first and last name. What I need to do is a query that is ordered by priority: who is in lista1 comes first, then who is in lista2 and lastly what is not only in the 3 field document. Can anyone help me?

UPDATE: joining everything in the same collection is not an option, and in SQL it's done that way link

    
asked by anonymous 19.08.2017 / 04:30

1 answer

0

Following the specs you gave in the comment, I would add all the information to a collection only.

It seems to me that you have three lists as input. You could put the priority as a field within the document:

{
   "cpf" : 12345678900,
   "nome" : "Astolfo"
   "prioridade" : 1
},
{
   "cpf" : 98765432100,
   "nome" : "João"
   "prioridade" : 2
}

I considered that Astolfo was in list 1 and John in list 2. You could make indexes with the fields you need to search, per CPF for example, and the priority field. This way you need to maintain, query and update only one collection. At the end, just order the query by priority:

      db.pessoas.find({"cpf":12345678900},{"prioridade":1})

If there are two documents with the same CPF in the collection they will appear in the order of priority.

    
19.08.2017 / 16:44