Salta el contingut

DAM - DAW - MP 0484 Bases de Dades

Cerca de documents amb find

db.collection.find

Podem filtrar les dades d’una col·lecció i projectar només alguns camps i visualitzar-ne només algunes files. També podem ordenar les dades i limitar el nombre de documents de sortida (clarificar la sortida).

Treballarem inicialment amb les dades dels exemples de la introducció.

Instruccions de cerca – find

Sintaxi
db.collection.find(query, projection, options)

Podem trobar alguns exemples a la web de mongoDB. Cal escollir, al desplegable, com a language, MongoDB Shell.

Sense filtre
db.inventory.find()
Possible Sortida
[
     {
          _id: ObjectId("6811ffa34f9158e3e30b96e1"),
          item: 'canvas',
          qty: 100,
          size: { h: 28, w: 35.5, uom: 'cm' },
          status: 'A'
     },
     {
          _id: ObjectId("6811ffa34f9158e3e30b96e2"),
          item: 'journal',
          qty: 25,
          size: { h: 14, w: 21, uom: 'cm' },
          status: 'A'
     },
     {
          _id: ObjectId("6811ffa34f9158e3e30b96e3"),
          item: 'mat',
          qty: 85,
          size: { h: 27.9, w: 35.5, uom: 'cm' },
          status: 'A'
     },
     {
          _id: ObjectId("6811ffa34f9158e3e30b96e4"),
          item: 'mousepad',
          qty: 25,
          size: { h: 19, w: 22.85, uom: 'cm' },
          status: 'P'
     },
     {
          _id: ObjectId("6811ffa34f9158e3e30b96e5"),
          item: 'notebook',
          qty: 50,
          size: { h: 8.5, w: 11, uom: 'in' },
          status: 'P'
     },
     {
          _id: ObjectId("6811ffa34f9158e3e30b96e6"),
          item: 'paper',
          qty: 100,
          size: { h: 8.5, w: 11, uom: 'in' },
          status: 'D'
     },
     {
          _id: ObjectId("6811ffa34f9158e3e30b96e7"),
          item: 'planner',
          qty: 75,
          size: { h: 22.85, w: 30, uom: 'cm' },
          status: 'D'
     },
     {
          _id: ObjectId("6811ffa34f9158e3e30b96e8"),
          item: 'postcard',
          qty: 45,
          size: { h: 10, w: 15.25, uom: 'cm' },
          status: 'A'
     },
     {
          _id: ObjectId("6811ffa34f9158e3e30b96e9"),
          item: 'sketchbook',
          qty: 80,
          size: { h: 14, w: 21, uom: 'cm' },
          status: 'A'
     },
     {
          _id: ObjectId("6811ffa34f9158e3e30b96ea"),
          item: 'sketch pad',
          qty: 95,
          size: { h: 22.85, w: 30.5, uom: 'cm' },
          status: 'A'
     }
]
Valor d’un camp
db.inventory.find( { status : "A" } )
Possible Sortida
[
     {
          _id: ObjectId("6811ffa34f9158e3e30b96e1"),
          item: 'canvas',
          qty: 100,
          size: { h: 28, w: 35.5, uom: 'cm' },
          status: 'A'
     },
     {
          _id: ObjectId("6811ffa34f9158e3e30b96e2"),
          item: 'journal',
          qty: 25,
          size: { h: 14, w: 21, uom: 'cm' },
          status: 'A'
     },
     {
          _id: ObjectId("6811ffa34f9158e3e30b96e3"),
          item: 'mat',
          qty: 85,
          size: { h: 27.9, w: 35.5, uom: 'cm' },
          status: 'A'
     },
     {
          _id: ObjectId("6811ffa34f9158e3e30b96e8"),
          item: 'postcard',
          qty: 45,
          size: { h: 10, w: 15.25, uom: 'cm' },
          status: 'A'
     },
     {
          _id: ObjectId("6811ffa34f9158e3e30b96e9"),
          item: 'sketchbook',
          qty: 80,
          size: { h: 14, w: 21, uom: 'cm' },
          status: 'A'
     },
     {
          _id: ObjectId("6811ffa34f9158e3e30b96ea"),
          item: 'sketch pad',
          qty: 95,
          size: { h: 22.85, w: 30.5, uom: 'cm' },
          status: 'A'
     }
]
Valor de 2 camps (AND)
db.inventory.find( { status : "A" , item: "journal"} )
Possible Sortida
[
     {
          _id: ObjectId("6811ffa34f9158e3e30b96e2"),
          item: 'journal',
          qty: 25,
          size: { h: 14, w: 21, uom: 'cm' },
          status: 'A'
     }
]
Valor d’un camp d’un subdocument
db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )
Possible Sortida
[
     {
          _id: ObjectId("6811ffa34f9158e3e30b96e2"),
          item: 'journal',
          qty: 25,
          size: { h: 14, w: 21, uom: 'cm' },
          status: 'A'
     },
     {
          _id: ObjectId("6811ffa34f9158e3e30b96e9"),
          item: 'sketchbook',
          qty: 80,
          size: { h: 14, w: 21, uom: 'cm' },
          status: 'A'
     }
]
Valor d’un camp d’un subdocument
db.inventory.find( { "size.w": 35.5 }  )
Possible Sortida
[
     {
          _id: ObjectId("6811ffa34f9158e3e30b96e1"),
          item: 'canvas',
          qty: 100,
          size: { h: 28, w: 35.5, uom: 'cm' },
          status: 'A'
     },
     {
          _id: ObjectId("6811ffa34f9158e3e30b96e3"),
          item: 'mat',
          qty: 85,
          size: { h: 27.9, w: 35.5, uom: 'cm' },
          status: 'A'
     }
]

Per accedir a valors d'un subdocument utilitzem la notació punt:

"<embedded document>.<field>"

Els camps, separats per punt, i entre cometes dobles.