0

I am quite new in using Mongodb. I have a database exported from a json (the other values at the moment are not important).

The query I am trying to make is this one: I want all the mercancia that all the different client have. So in this case, the client Electronica Chispas, will have 2 mercancia with all the info about it.

[{"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 missing some syntaxis or maybe I am just not doing it right. In python (but you can do it in the db itself).

db.prueba1.find({'cliente.cliente': {$mercancias}})

I have syntax error, but there are so many ways to do find() that I am quite lost. I am not looking especifically for the query solved, but the way it could be solved (pseudocode, whatever helps me solve it).

3
  • 2
    You only use $ with mongo provided operations not on field or values. Commented Mar 15, 2018 at 13:22
  • db.prueba1.find({'cliente. nombre': 'Cafes el amanencer'}) this will match the second document Commented Mar 15, 2018 at 13:23
  • Won't this show me all the info of client, but not the "mercancia" nor "vagon"? Or it will show it? But if it shows it, it will show also the "vagon" info, and I do not want that @harshil9968 Commented Mar 15, 2018 at 13:26

1 Answer 1

1

By default MongoDB is going to return the entire document. If you only want part of the document you can use a projection.

Try this:

db.prueba1.find({}, {mercancia: 1})

It should return:

{
  { _id: id-of-doc-one, mercancia: { ... } },
  { _id: id-of-doc-two, mercancia: { ... } },
  { _id: id-of-doc-three, mercancia: { ... } }
}

If you want the mercancia of a specific document you can do something like:

db.prueba1.find( {'client.nombre', 'Cafes el amanencer'}, {_id: 0, mercancia: 1 });

Which would return:

{
    mercancia: {
        envio: "Normal",
        tipo: "Gaseoso", 
        fecha: "24/12/2003",
        peso: 21,
        volumen: 43,
        origen: "Cadiz",
        destino: "Castellon"
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

So in case I want ALL the "mercancia" from all clients, there is no way to do that?
I think that is the first thing I listed. If you want them all in one array then you may need to do that yourself on the server.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.