Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. // Realizar consultas sobre documentos
  2.  
  3. function mostrarDatos(cursor) {
  4. while(cursor.hasNext()) {
  5. printjson(cursor.next());
  6. }
  7. }
  8.  
  9. function consultar_todo() {
  10.  
  11. var cursor = db.alumnos.find(
  12. // condiciones de la consulta
  13. {} // todos los documentos
  14. );
  15.  
  16. print("Número de alumnos: " + cursor.count());
  17.  
  18. mostrarDatos(cursor);
  19. }
  20.  
  21. // consultar_todo();
  22.  
  23. function consultar_un_documento() {
  24.  
  25. // SOLO recuperamos el primer documento de la colección
  26. printjson(
  27. db.alumnos.findOne(
  28. {}
  29. )
  30. );
  31. }
  32.  
  33. // consultar_un_documento();
  34.  
  35. function consultar_con_filtro() {
  36.  
  37. var cursor = db.alumnos.find(
  38. /////////////////////////////////////////
  39. // {
  40. // creditos : 20
  41. // }
  42. /////////////////////////////////////////
  43. // Por defecto se emplea el AND en más de una condición
  44. // {
  45. // creditos : { $gte : 60},
  46. // "domicilio.provincia" : "Madrid"
  47. // }
  48. /////////////////////////////////////////
  49. // {
  50. // $and : [
  51. // { creditos : { $gte : 60} },
  52. // { "domicilio.provincia" : "Madrid"}
  53. // ]
  54. // }
  55. /////////////////////////////////////////
  56. // {
  57. // $or : [
  58. // { creditos : 40},
  59. // { creditos : 60 },
  60. // { nombre : /2$/ }
  61. // ]
  62. // }
  63. /////////////////////////////////////////
  64. {
  65. creditos : { $not : { $in : [40, 60, 80] }}
  66. }
  67. );
  68.  
  69. mostrarDatos(cursor);
  70. }
  71.  
  72. // consultar_con_filtro();
  73.  
  74. function consulta_limitando_propiedades() {
  75.  
  76. mostrarDatos(
  77. db.alumnos.find(
  78. {}, // filtro
  79. ///////////////////////////////////////////
  80. // Propiedades recuperadas del documento
  81. // {
  82. // nombre : 1,
  83. // apellidos : 1,
  84. // "domicilio.provincia" : 1,
  85. // _id : 0
  86. // }
  87. ////////////////////////////
  88. {
  89. domicilio : 0,
  90. cursos : 0
  91. }
  92. )
  93. );
  94. }
  95.  
  96. // consulta_limitando_propiedades();
  97.  
  98. function consultar_matrices() {
  99.  
  100. mostrarDatos(
  101. db.alumnos.find(
  102. /////////////////////////////////////////////
  103. // Por valores exactos incluyendo la posición
  104. {
  105. cursos : [100, 200, 300]
  106. }
  107. )
  108. );
  109. }
  110.  
  111. consultar_matrices();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement