Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. // Inserción de documentos
  2.  
  3. // Insertar un documento
  4. // Si no incluimos _id lo crea MongoDB ObjectId
  5.  
  6. function altaDeUnDocumento() {
  7. db.alumnos.insertOne(
  8. {
  9. nombre : "Juan",
  10. apellidos : "García",
  11. domicilio : {
  12. nombre : "C/ Cronos, 10",
  13. provincia : "Madrid"
  14. },
  15. creditos : 10
  16. }
  17. );
  18. }
  19.  
  20. function consultarAlumnos() {
  21. var cursor = db.alumnos.find({});
  22. while(cursor.hasNext()) {
  23. printjson(cursor.next());
  24. }
  25. }
  26.  
  27. // altaDeUnDocumento();
  28. // consultarAlumnos();
  29.  
  30. // Insertar varios documentos
  31. // POR DEFECTO NO HAY TRANSACCION
  32.  
  33. function altaDeVariosDocumentos() {
  34.  
  35. db.alumnos.drop();
  36.  
  37. var alumnos = [];
  38. for(var i = 1; i <= 10; i++) {
  39. alumnos.push(
  40. {
  41. nombre : "Nombre " + i,
  42. apellidos : "García",
  43. fechaAlta : new Date(),
  44. domicilio : {
  45. nombre : "C/ Cronos, 10",
  46. provincia : "Madrid"
  47. },
  48. creditos : i % 2 == 0? 10 * i : null,
  49. nivel : i % 2 == 0? 10 : "10",
  50. cursos : [ i * 10, i * 20, i * 30]
  51. }
  52. );
  53. }
  54.  
  55. db.alumnos.insertMany(alumnos);
  56. }
  57.  
  58. // altaDeVariosDocumentos();
  59. // consultarAlumnos();
  60.  
  61. // Podemos incorporar retricciones en el consumo de
  62. // almacenamiento de una colección:
  63. // tamaño total
  64. // número de documentos
  65. function empleoDeColeccionCapped() {
  66.  
  67. db.pedidos.drop();
  68.  
  69. db.createCollection(
  70. // Nombre de la colección
  71. "pedidos",
  72. // Opciones de creación
  73. // en este caso "Capped"
  74. {
  75. capped : true,
  76. // Tamaño máximo en bytes de la colección
  77. size : 16777216, // 16m
  78. // Número máximo de documentos en la colección
  79. max : 10
  80. }
  81. );
  82.  
  83. for(var i = 1; i <= 20; i++) {
  84. db.pedidos.insertOne(
  85. {
  86. concepto : "Pedido no. " + i
  87. }
  88. );
  89. }
  90.  
  91. var contador = db.pedidos.countDocuments({});
  92. print("Número de pedidos: " + contador);
  93.  
  94. var cursorPedidos = db.pedidos.find({});
  95. while(cursorPedidos.hasNext()) {
  96. printjson(cursorPedidos.next());
  97. }
  98. }
  99.  
  100. // empleoDeColeccionCapped();
  101.  
  102. function crearColeccionConValidaciones() {
  103.  
  104. db.clientes.drop();
  105.  
  106. db.createCollection(
  107. "clientes",
  108. {
  109. // Las restricciones se indican mediante
  110. // un filtro de consulta
  111. validator : {
  112. $and : [
  113. {
  114. nombre : { $regex : /^[a-zA-Z ñÑ]{3,12}$/}
  115. },
  116. {
  117. edad : { $type : "number"}
  118. },
  119. {
  120. registrado : { $in : ["si", "no"]}
  121. }
  122. ]
  123. }
  124. }
  125. );
  126.  
  127.  
  128. // Debería funcionar
  129. try {
  130. db.clientes.insertOne(
  131. {
  132. nombre : "Jose Luis",
  133. edad : 26,
  134. registrado : "no",
  135. apellidos : "Cualquier cosa $//.@"
  136. }
  137. );
  138. print("Cliente 1 creado ...");
  139. } catch (error) {
  140. print("Cliente 1 no creado ...");
  141. printjson(error);
  142. }
  143.  
  144. // No debería funcionar
  145. try {
  146. db.clientes.insertOne(
  147. {
  148. nombre : "Jose Luis 2", // mal
  149. edad : 26,
  150. registrado : "no",
  151. apellidos : "Cualquier cosa $//.@"
  152. }
  153. );
  154. print("Cliente 2 creado ...");
  155. } catch (error) {
  156. print("Cliente 2 no creado ...");
  157. printjson(error);
  158. }
  159.  
  160. }
  161.  
  162. // crearColeccionConValidaciones();
  163.  
  164. function crearColeccionConValidacionesJSONSchema() {
  165.  
  166. db.billetes.drop();
  167.  
  168. db.createCollection(
  169. "billetes",
  170. {
  171. validator : {
  172. $jsonSchema : {
  173. bsonType : "object",
  174. required : ["origen", "destino", "cliente"],
  175. properties : {
  176. origen : {
  177. bsonType : "string",
  178. description : "Código aeropuerto origen"
  179. },
  180. destino : {
  181. bsonType : "string",
  182. description : "Código aeropuerto destino"
  183. },
  184. cliente : {
  185. bsonType : "string",
  186. enum : ["empresa", "particular", "agencia"],
  187. description : "Tipo de cliente"
  188. }
  189. }
  190. }
  191. }
  192. }
  193. );
  194.  
  195. // Debería funcionar
  196. try {
  197. db.billetes.insertOne(
  198. {
  199. origen : "MAD",
  200. destino : "LAX",
  201. cliente : "empresa",
  202. importe : 10000
  203. }
  204. );
  205. print("Billete 1 creado ...");
  206. } catch (error) {
  207. print("Billete 1 no creado ...");
  208. printjson(error);
  209. }
  210.  
  211. // No debería funcionar
  212. try {
  213. db.billetes.insertOne(
  214. {
  215. origen : "MAD",
  216. destino : "LAX",
  217. cliente : "mayorista", // mal
  218. importe : 10000
  219. }
  220. );
  221. print("Billete 2 creado ...");
  222. } catch (error) {
  223. print("Billete 2 no creado ...");
  224. printjson(error);
  225. }
  226.  
  227. }
  228.  
  229. // crearColeccionConValidacionesJSONSchema();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement