Diferència entre revisions de la pàgina «MongoDB»
Salta a la navegació
Salta a la cerca
| Línia 9: | Línia 9: | ||
* Mongo shell permet JavaScript. Creem una col·lecció de dades: | * Mongo shell permet JavaScript. Creem una col·lecció de dades: | ||
*: > for (i=0;i<1000;i++) { names=["examen","treball","questionari"]; for(j=0;j<3;j++) {db.notes.insert( {"estudiant":i,"tipus": names[j], nota : Math.round(Math.random()*100) }); }} | *: > for (i=0;i<1000;i++) { names=["examen","treball","questionari"]; for(j=0;j<3;j++) {db.notes.insert( {"estudiant":i,"tipus": names[j], nota : Math.round(Math.random()*100) }); }} | ||
| + | |||
| Línia 16: | Línia 17: | ||
* Importar-lo:<pre>$ mongoimport -d students -c grades < grades.ef42a2b3e7ff.js</pre> | * Importar-lo:<pre>$ mongoimport -d students -c grades < grades.ef42a2b3e7ff.js</pre> | ||
* | * | ||
| + | |||
| + | === CRUD === | ||
| + | Create Retrieve Update Delete | ||
| + | |||
| + | Retrieve (select): FIND | ||
| + | > db.<coleccio>.findOne() | ||
| + | > db.<coleccio>.findOne( {"tipus":"examen"} ) | ||
| + | > db.<coleccio>.find() | ||
| + | > db.<coleccio>.find( {"tipus":"examen"} ) | ||
== Exercicis == | == Exercicis == | ||
... | ... | ||
Revisió del 12:29, 4 des 2013
Introducció
- Instal·lar darrera versió (2.4.8) a Ubuntu:
- Monogo shell:
$ mongo
- Seleccionar una db (o crear):
> use <nom_db>
- Mongo shell permet JavaScript. Creem una col·lecció de dades:
- > for (i=0;i<1000;i++) { names=["examen","treball","questionari"]; for(j=0;j<3;j++) {db.notes.insert( {"estudiant":i,"tipus": names[j], nota : Math.round(Math.random()*100) }); }}
- Exemple amb dades:
- Importar-lo:
$ mongoimport -d students -c grades < grades.ef42a2b3e7ff.js
CRUD
Create Retrieve Update Delete
Retrieve (select): FIND
> db.<coleccio>.findOne()
> db.<coleccio>.findOne( {"tipus":"examen"} )
> db.<coleccio>.find()
> db.<coleccio>.find( {"tipus":"examen"} )
Exercicis
...