Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Inserción de documentos
- // Insertar un documento
- // Si no incluimos _id lo crea MongoDB ObjectId
- function altaDeUnDocumento() {
- db.alumnos.insertOne(
- {
- nombre : "Juan",
- apellidos : "García",
- domicilio : {
- nombre : "C/ Cronos, 10",
- provincia : "Madrid"
- },
- creditos : 10
- }
- );
- }
- function consultarAlumnos() {
- var cursor = db.alumnos.find({});
- while(cursor.hasNext()) {
- printjson(cursor.next());
- }
- }
- // altaDeUnDocumento();
- // consultarAlumnos();
- // Insertar varios documentos
- // POR DEFECTO NO HAY TRANSACCION
- function altaDeVariosDocumentos() {
- db.alumnos.drop();
- var alumnos = [];
- for(var i = 1; i <= 10; i++) {
- alumnos.push(
- {
- nombre : "Nombre " + i,
- apellidos : "García",
- fechaAlta : new Date(),
- domicilio : {
- nombre : "C/ Cronos, 10",
- provincia : "Madrid"
- },
- creditos : i % 2 == 0? 10 * i : null,
- nivel : i % 2 == 0? 10 : "10",
- cursos : [ i * 10, i * 20, i * 30]
- }
- );
- }
- db.alumnos.insertMany(alumnos);
- }
- // altaDeVariosDocumentos();
- // consultarAlumnos();
- // Podemos incorporar retricciones en el consumo de
- // almacenamiento de una colección:
- // tamaño total
- // número de documentos
- function empleoDeColeccionCapped() {
- db.pedidos.drop();
- db.createCollection(
- // Nombre de la colección
- "pedidos",
- // Opciones de creación
- // en este caso "Capped"
- {
- capped : true,
- // Tamaño máximo en bytes de la colección
- size : 16777216, // 16m
- // Número máximo de documentos en la colección
- max : 10
- }
- );
- for(var i = 1; i <= 20; i++) {
- db.pedidos.insertOne(
- {
- concepto : "Pedido no. " + i
- }
- );
- }
- var contador = db.pedidos.countDocuments({});
- print("Número de pedidos: " + contador);
- var cursorPedidos = db.pedidos.find({});
- while(cursorPedidos.hasNext()) {
- printjson(cursorPedidos.next());
- }
- }
- // empleoDeColeccionCapped();
- function crearColeccionConValidaciones() {
- db.clientes.drop();
- db.createCollection(
- "clientes",
- {
- // Las restricciones se indican mediante
- // un filtro de consulta
- validator : {
- $and : [
- {
- nombre : { $regex : /^[a-zA-Z ñÑ]{3,12}$/}
- },
- {
- edad : { $type : "number"}
- },
- {
- registrado : { $in : ["si", "no"]}
- }
- ]
- }
- }
- );
- // Debería funcionar
- try {
- db.clientes.insertOne(
- {
- nombre : "Jose Luis",
- edad : 26,
- registrado : "no",
- apellidos : "Cualquier cosa $//.@"
- }
- );
- print("Cliente 1 creado ...");
- } catch (error) {
- print("Cliente 1 no creado ...");
- printjson(error);
- }
- // No debería funcionar
- try {
- db.clientes.insertOne(
- {
- nombre : "Jose Luis 2", // mal
- edad : 26,
- registrado : "no",
- apellidos : "Cualquier cosa $//.@"
- }
- );
- print("Cliente 2 creado ...");
- } catch (error) {
- print("Cliente 2 no creado ...");
- printjson(error);
- }
- }
- // crearColeccionConValidaciones();
- function crearColeccionConValidacionesJSONSchema() {
- db.billetes.drop();
- db.createCollection(
- "billetes",
- {
- validator : {
- $jsonSchema : {
- bsonType : "object",
- required : ["origen", "destino", "cliente"],
- properties : {
- origen : {
- bsonType : "string",
- description : "Código aeropuerto origen"
- },
- destino : {
- bsonType : "string",
- description : "Código aeropuerto destino"
- },
- cliente : {
- bsonType : "string",
- enum : ["empresa", "particular", "agencia"],
- description : "Tipo de cliente"
- }
- }
- }
- }
- }
- );
- // Debería funcionar
- try {
- db.billetes.insertOne(
- {
- origen : "MAD",
- destino : "LAX",
- cliente : "empresa",
- importe : 10000
- }
- );
- print("Billete 1 creado ...");
- } catch (error) {
- print("Billete 1 no creado ...");
- printjson(error);
- }
- // No debería funcionar
- try {
- db.billetes.insertOne(
- {
- origen : "MAD",
- destino : "LAX",
- cliente : "mayorista", // mal
- importe : 10000
- }
- );
- print("Billete 2 creado ...");
- } catch (error) {
- print("Billete 2 no creado ...");
- printjson(error);
- }
- }
- // crearColeccionConValidacionesJSONSchema();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement