M.K
M.K

Reputation: 1505

simple Find() query in MongoDB

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).

Upvotes: 0

Views: 985

Answers (1)

Pace
Pace

Reputation: 43957

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"
    }
}

Upvotes: 1

Related Questions