{"id":604,"date":"2023-10-23T10:33:04","date_gmt":"2023-10-23T08:33:04","guid":{"rendered":"http:\/\/eines-informatiques.recursos.uoc.edu\/gestion-de-datos\/?page_id=604"},"modified":"2025-07-26T18:56:53","modified_gmt":"2025-07-26T16:56:53","slug":"2-6-buscar-documents","status":"publish","type":"page","link":"http:\/\/eines-informatiques.recursos.uoc.edu\/gestion-de-datos\/2-6-buscar-documents\/","title":{"rendered":"2.6. Buscar documents"},"content":{"rendered":"<p>Primer comptarem quants documents t\u00e9 la col\u00b7lecci\u00f3 chr1 amb la instrucci\u00f3<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mariadb\" data-enlighter-theme=\"mowtwo\">db.chr1.find().count();<\/pre>\n<p>Aquesta instrucci\u00f3 cont\u00e9 dues funcions. La funci\u00f3 <code>find()<\/code> que \u00e9s la funci\u00f3 que utilitzem per realitzar recerques en la col\u00b7lecci\u00f3 de documents. Seria la instrucci\u00f3 <strong>SELECT<\/strong> que hem vist a MySQL. Si no hi afegim par\u00e0metres ens mostrar\u00e0 tots els documents de la col\u00b7lecci\u00f3, si hi afegim par\u00e0metres, aquests seran els criteris de recerca o els filtres de la selecci\u00f3. En aquest cas complementem la funci\u00f3 <code>find()<\/code> amb la funci\u00f3 <code>count()<\/code> per comptar els documents trobats. La funci\u00f3 <code>count()<\/code> no requereix par\u00e0metres, per\u00f2 cal escriure els par\u00e8ntesis igualment.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mariadb\" data-enlighter-theme=\"mowtwo\">&gt; db.chr1.find().count();\r\n3<\/pre>\n<p>En executar la instrucci\u00f3 <code>db.chr1.find().count();<\/code> el sistema ens en retorna 3. La col\u00b7lecci\u00f3 <strong>chr1<\/strong> t\u00e9 3 documents.<\/p>\n<p>Utilitzarem la instrucci\u00f3 <code>db.chr1.find();<\/code> per veure tots els documents:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mariadb\" data-enlighter-theme=\"mowtwo\">&gt; db.chr1.find():\r\n{ \"_id\" : ObjectId(\"64861afc32e0aa299185905c\"), \"name\" : \"uc001aaa\r\narts\" : 11873m12612,13220,\", \"exonEnds\" : \"12227,12721,14409,\", \"\r\n{ \"_id\" : ObjectId(\"64861df75d6ce7147d325b7e\"), \"name\" : \"uc010nxr\r\narts\" : \"11873,12645,13220,\", \"exonEnds\" : \"12227,12697,14409,\", \"\r\n{ \"_id\" : ObjectId(\"64861e025d6ce7147d325b7f\"), \"name\" : \"uc009vit\r\narts\" : \"14361,14969,15795,16606,16857,17232,17914,18267,18912,\",\r\n&gt;<\/pre>\n<p>Per a cada document el sistema ens mostra el camp <strong>_id<\/strong> amb un valor generat aleat\u00f2riament per la funci\u00f3 del sistema <code>ObjectId();<\/code><\/p>\n<p>En el camp <strong>_id<\/strong> es guarda la clau prim\u00e0ria del document. Aquesta clau prim\u00e0ria \u00e9s tan important per al sistema que si el document que inserim no t\u00e9 el camp <strong>_id<\/strong> el sistema el crea de forma autom\u00e0tica. En els nostres exemples els tres documents inserits no tenen el camp <strong>_id<\/strong> i el sistema l\u2019ha generat autom\u00e0ticament.<\/p>\n<p>En un document hi pot haver molts camps i, tal com els mostra la funci\u00f3 <code>find();<\/code> , queden poc llegibles. Perqu\u00e8 el document es pugui llegir millor, la funci\u00f3 <code>find();<\/code> es pot complementar amb la funci\u00f3 <code>pretty();<\/code><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mariadb\" data-enlighter-theme=\"mowtwo\">db.chr1.find().pretty();<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mariadb\" data-enlighter-theme=\"mowtwo\">&gt; db.chr1.find().pretty();\r\n{\r\n        \"_id\" : ObjectId(\"64861afc32e0aa299185905c\"),\r\n        \"name\" : \"uc001aaa.3\",\r\n        \"chrom\" : \"chr1\",\r\n        \"strand\" : \"+\",\r\n        \"txStart\" : 11873,\r\n        \"txEnd\" : 14409,\r\n        \"cdsStart\" : 11873,\r\n        \"cdsEnd\" : 11873,\r\n        \"exonCount\" : 3,\r\n        \"exonStarts\" : \"11873,12612,13220,\",\r\n        \"exonEnds\" : \"12227,12721,14409,\",\r\n        \"proteinID\" : \"\",\r\n        \"alignID\" : \"uc001aaa.3\"\r\n}\r\n{\r\n        \"_id\" : ObjectId(\"64861df75d6ce7147d325b7e\"),\r\n        \"name\" : \"uc010nxr.1\",\r\n        \"chrom\" : \"chr1\",\r\n        \"strand\" : \"+\",\r\n        \"txStart\" : 11873,\r\n        \"txEnd\" : 14409,\r\n        \"cdsStart\" : 11873,\r\n        \"cdsEnd\" : 11873,\r\n        \"exonCount\" : 3,\r\n        \"exonStarts\" : \"11873,12645,13220,\",\r\n        \"exonEnds\" : \"12227,12697,14409,\",\r\n        \"proteinID\" : \"\",\r\n        \"alignID\" : \"uc010nxr.1\"\r\n}<\/pre>\n<p>Com podem observar, la funci\u00f3 <code>pretty();<\/code> ens permet veure els valors de cada camp de cada document d\u2019una forma molt m\u00e9s amigable.<\/p>\n<p>Vegem ara alguns exemples d\u2019utilitzaci\u00f3 de la funci\u00f3 <code>find()<\/code> amb alguns par\u00e0metres per filtrar els resultats.<\/p>\n<p>Mostrarem nom\u00e9s els gens de la nostra col\u00b7lecci\u00f3 que es troben en la cadena <code>+<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mariadb\" data-enlighter-theme=\"mowtwo\">db.chr1.find({\"strand\": \"+\"}).pretty();<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mariadb\" data-enlighter-theme=\"mowtwo\">&gt; db.chr1.find({\"strand\": \"+\").pretty();\r\n{\r\n        \"_id\" : ObjectId(\"6487c3e05d6ce7147d325b83\"),\r\n        \"name\" : \"uc001aaa.3\",\r\n        \"chrom\" : \"chr1\",\r\n        \"strand\" : \"+\",\r\n        \"txStart\" : 11873,\r\n        \"txEnd\" : 14409,\r\n        \"cdsStart\" : 11873,\r\n        \"cdsEnd\" : 11873,\r\n        \"exonCount\" : 3,\r\n        \"exonStarts\" : \"11873,12612,13220,\",\r\n        \"exonEnds\" : \"12227,12721,14409,\",\r\n        \"proteinID\" : \"\",\r\n        \"alignID\" : \"uc001aaa.3\"\r\n}\r\n{\r\n        \"_id\" : ObjectId(\"6487c410235597cf4bc92782\"),\r\n        \"name\" : \"uc010nxr.1\",\r\n        \"chrom\" : \"chr1\",\r\n        \"strand\" : \"+\",\r\n        \"txStart\" : 11873,\r\n        \"txEnd\" : 14409,\r\n        \"cdsStart\" : 11873,\r\n        \"cdsEnd\" : 11873,\r\n        \"exonCount\" : 3,\r\n        \"exonStarts\" : \"11873,12645,13220,\",\r\n        \"exonEnds\" : \"12227,12697,14409,\",\r\n        \"proteinID\" : \"\",\r\n        \"alignID\" : \"uc010nxr.1\"\r\n}<\/pre>\n<p>El par\u00e0metre de la funci\u00f3 \u00e9s la condici\u00f3 de selecci\u00f3 de la recerca <code>{\"strand\": \"+\"}<\/code><\/p>\n<p>Si no volem mostrar algun camp, com per exemple el camp <strong><code>_id<\/code><\/strong>, escrivim<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mariadb\" data-enlighter-theme=\"mowtwo\">db.chr1.find({\"strand\": \"+\"}, {\"_id\":0}).pretty();\r\n\r\n<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mariadb\" data-enlighter-theme=\"mowtwo\">&gt; db.chr1.find({\"strand\": \"+\"), {\"\u2013id\":0})pretty();\r\n{\r\n        \"name\" : \"uc001aaa.3\",\r\n        \"chrom\" : \"chr1\",\r\n        \"strand\" : \"+\",\r\n        \"txStart\" : 11873,\r\n        \"txEnd\" : 14409,\r\n        \"cdsStart\" : 11873,\r\n        \"cdsEnd\" : 11873,\r\n        \"exonCount\" : 3,\r\n        \"exonStarts\" : \"11873,12612,13220,\",\r\n        \"exonEnds\" : \"12227,12721,14409,\",\r\n        \"proteinID\" : \"\",\r\n        \"alignID\" : \"uc001aaa.3\"\r\n}\r\n{\r\n        \"name\" : \"uc010nxr.1\",\r\n        \"chrom\" : \"chr1\",\r\n        \"strand\" : \"+\",\r\n        \"txStart\" : 11873,\r\n        \"txEnd\" : 14409,\r\n        \"cdsStart\" : 11873,\r\n        \"cdsEnd\" : 11873,\r\n        \"exonCount\" : 3,\r\n        \"exonStarts\" : \"11873,12645,13220,\",\r\n        \"exonEnds\" : \"12227,12697,14409,\",\r\n        \"proteinID\" : \"\",\r\n        \"alignID\" : \"uc010nxr.1\"\r\n}<\/pre>\n<p>Si volem mostrar un sol camp dels documents seleccionats, com per exemple el camp <strong>name<\/strong>, escrivim<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mariadb\" data-enlighter-theme=\"mowtwo\">db.chr1.find({\"strand\": \"+\"},{\"name\":1, \"_id\":0 }).pretty();<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mariadb\" data-enlighter-theme=\"mowtwo\">&gt; db.chr1.find({\"strand\": \"+\"),{\"name\":1, \"\u2013id\":0 }).pretty();\r\n{ \"name\" : \"uc001aaa.3\" }\r\n{ \"name\" : \"uc010nxr.1\" }<\/pre>\n<p>Les consultes amb la funci\u00f3 <code>find()<\/code> poden ser molt m\u00e9s elaborades, usant diversos camps com a condici\u00f3 de recerca, o tamb\u00e9 usant operadors especials com major que, menor que, etc.<\/p>\n<div class=\"featured featured-grey\"><\/p>\n<p><strong>Exemples d\u2019operadors especials:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"droide\">$gt, $gte, $lt, $lte, $ne, $in, $nin, $mod, $regex\/$options,<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" data-enlighter-theme=\"droide\">$all, $size, $exists, $type, $not, $or, $nor, $elemMatch<\/pre>\n<p>Exemple de selecci\u00f3 de documents en qu\u00e8 el camp <strong>txStart<\/strong> sigui major que 11873:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mariadb\" data-enlighter-theme=\"mowtwo\">db.chr1.find({\"txStart\": {\"$gt\" : 11873 }}).pretty();<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mariadb\" data-enlighter-theme=\"mowtwo\">&gt; db.chr1.find({\"txStart\": {$gt\" : 11873 }}).pretty();\r\n{\r\n        \"_id\" : ObjectId(\"6487c41c235597cf4bc92783\"),\r\n        \"name\" : \"uc009vit.3\",\r\n        \"chrom\" : \"chr1\",\r\n        \"strand\" : \"-\",\r\n        \"txStart\" : 14361,\r\n        \"txEnd\" : 19759,\r\n        \"cdsStart\" : 14361,\r\n        \"cdsEnd\" : 14361,\r\n        \"exonCount\" : 9,\r\n        \"exonStarts\" : \"14361,14969,15795,16606,16857,17232,17914,18267,18912,\",\r\n        \"exonEnds\" : \"14829,15038,15947,16765,17055,17742,18061,18366,19759,\",\r\n        \"proteinID\" : \"\",\r\n        \"alignID\" : \"uc009vit.3\"\r\n}\r\n&gt;<\/pre>\n<p>O que compleixi dues condicions, que es trobin en la cadena + i que el camp <strong>txStart<\/strong> sigui major o igual a 11873.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mariadb\" data-enlighter-theme=\"mowtwo\">db.chr1.find({\"strand\": \"+\", \"txStart\": {\"$gte\" : 11873 }}).pretty();<\/pre>\n<p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Primer comptarem quants documents t\u00e9 la col\u00b7lecci\u00f3 chr1 amb la instrucci\u00f3 db.chr1.find().count(); Aquesta instrucci\u00f3 cont\u00e9 dues funcions. La funci\u00f3 find() que \u00e9s la funci\u00f3 que utilitzem per realitzar recerques en la col\u00b7lecci\u00f3 de documents. Seria la instrucci\u00f3 SELECT que hem vist a MySQL. Si no hi afegim par\u00e0metres ens mostrar\u00e0 tots els documents de la [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"acf":[],"_links":{"self":[{"href":"http:\/\/eines-informatiques.recursos.uoc.edu\/gestion-de-datos\/wp-json\/wp\/v2\/pages\/604"}],"collection":[{"href":"http:\/\/eines-informatiques.recursos.uoc.edu\/gestion-de-datos\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/eines-informatiques.recursos.uoc.edu\/gestion-de-datos\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/eines-informatiques.recursos.uoc.edu\/gestion-de-datos\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/eines-informatiques.recursos.uoc.edu\/gestion-de-datos\/wp-json\/wp\/v2\/comments?post=604"}],"version-history":[{"count":12,"href":"http:\/\/eines-informatiques.recursos.uoc.edu\/gestion-de-datos\/wp-json\/wp\/v2\/pages\/604\/revisions"}],"predecessor-version":[{"id":1059,"href":"http:\/\/eines-informatiques.recursos.uoc.edu\/gestion-de-datos\/wp-json\/wp\/v2\/pages\/604\/revisions\/1059"}],"wp:attachment":[{"href":"http:\/\/eines-informatiques.recursos.uoc.edu\/gestion-de-datos\/wp-json\/wp\/v2\/media?parent=604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}