0

I have a Database from a .data and these collections: clientes, mercancias and vagones. I want to find all the data of mercancias from the same client. So in this case, "Electronica Chispas" has 2 "mercancia" entities in the database:

[{"cliente": {"nombre": "Cafes el amanencer"},
 "mercancia": {"envio": "Normal", "tipo": "Gaseoso", "fecha": "24/12/2003", "peso": 21, "volumen": 43, "origen": "Cadiz", "destino": "Castellon"},
 "vagon": {"id": 1330, "volumen": 202, "peso": 433 }},{"cliente": {"nombre": "Electronica Chispas"}, "mercancia": {"envio": "Normal", "tipo": "Liquido", "fecha": "08/02/2005", "peso": 17, "volumen": 24, "origen": "San Sebastian", "destino": "Orense"}, "vagon": {"id": 1290, "volumen": 111, "peso": 464 }},{"cliente": {"nombre": "Electronica Chispas"}, "mercancia": {"envio": "Urgente intradia", "tipo": "Contaminante", "fecha": "15/09/2002", "peso": 11, "volumen": 83, "origen": "Valladolid", "destino": "Ciudad Real"}, "vagon": {"id": 1315, "volumen": 115, "peso": 481 }}]

I am trying this query:

db.mercancias.aggregate([{$lookup:{'from': 'clientes', 'localField':'destino', 'foreignField': 'nombre', 'as': 'clientes'}}])

But it is not working how I want to. Of course, I am missing to write the "Electronica Chispas" name somewhere in that query, but I am not sure where to write it.

peterh
  • 11,875
  • 18
  • 85
  • 108
M.K
  • 1,464
  • 2
  • 24
  • 46
  • Can you add documents from other collections ? What is your expected output ? – s7vr Mar 18 '18 at 13:49
  • My expected output is "Orense" and "Ciudad Real", or if I can get all the info of "mercancia", that's alright too. These 2 are the "destiny" of "Mercancias" from the person/client "electronica chispas". I know that the name "electronica chispas" is no where, but I dont know where to write it.@Veeram – M.K Mar 18 '18 at 14:35
  • I dont see any join key between clientes and mercancias. What is your criteria to join both collections ? I'm assuming the way you documents order in post are from different collections. – s7vr Mar 18 '18 at 15:18
  • Well, the 1st three lines (cliente, mercancias, vagon), have the same id in the db. That mercancia (we assume in real life) is from the client "cafes del amanecer". The other 2 mercancias are from "Electronica Chispas". So I want to get all the mercancias that are from 1 client providing its name (in this case, for example, "Electronica Chispas"). @Veeram – M.K Mar 18 '18 at 15:21
  • okay. I'm not sure why you are joining collection then. You can search the collection. Try `db.mercancias.aggregate([{$match:{'cliente.nombre': "Electronica Chispas"}}])` – s7vr Mar 18 '18 at 15:25
  • I did not know you could use anotherCollection.attribute to access another collection. That is why I was using Lookup. It does not work though. returns nothing. @Veeram – M.K Mar 18 '18 at 15:31
  • So you have to join to anotherColection on join key first before you can access the attribute from that collection. The way you have documents posted I can't tell how to join the mercancias to clientes collection. – s7vr Mar 18 '18 at 15:34
  • Did you get anywhere ? Let me know if I can help . – s7vr Mar 19 '18 at 15:30
  • As you said, making how to join was impossible. I realised it some hours ago, and decided to add in "mercancias" an id of the train and the name of the clientes, so the relation could be named. You were really helpful! I am going to ask another question about 2 queries I am having problems in a minute. Thanks a lot! @Veeram – M.K Mar 19 '18 at 15:32

1 Answers1

0

In MongoDB, having duplicate data is not important nor an issue!

Making how to join was impossible. I decided to add in mercancias an id of vagonand nombreof clientes, so the relation could be named. That way, there was no need to do LookUpin mongo, and a "simple" aggregate query could be done!

piet.t
  • 11,718
  • 21
  • 43
  • 52
M.K
  • 1,464
  • 2
  • 24
  • 46