1

I'm a MongoDB newbie, and I created the following JS file:

db=connection.getDB("TPMongo1028257");
var inversiones=[{
            categoria:"Accion",
            fdc:new Date(2019,07,05,0,0),
            iin: NumberDecimal ("5214.365"),
            cantidad:5,
            precioac: NumberDecimal ("9005.3659"),
            asesor:{
                nombre:"Carlos Solange",
                sitM:"Suba de las acciones",
                recom:"Comprar mas acciones",
},
            operacion:[{
                cat:"Accion",
                fdo:new Date(2019,10,15,0,0),
                importe: NumberDecimal ("9055.3659"),
                tipo:"Compra",
                asesor: "Carlos Solange"
            },
            {
                cat:"Accion",
                fdo:new Date(2019,11,05,0,0),
                importe: NumberDecimal ("9189.1789"),
                tipo:"Compra",
                asesor:"Carlos Solange"
}],                  
},
{
            categoria:"Bono",
            fdc:new Date(2018,12,17,0,0),
            iin: NumberDecimal ("9862.473"),
            cantidad:29,
            precioac: NumberDecimal ("9861.968"),
            asesor:{
                nombre:"Julio Rofri",
                sitM:"Se espera una gran baja en el precio de los bonos",
                recom:"Vender bonos"
            },
            operacion:{
                cat: "Bono",
                fdo:new Date(2019,01,20,0,0),
                importe: NumberDecimal ("9195.0254"),
                tipo:"Venta",
                asesor: "Julio Rofri"
            },

},
{
            categoria:"Bono",
            fdc:new Date(2018,10,9,0,0),
            iin: NumberDecimal ("11036.0365"),
            cantidad:15,
            precioac: NumberDecimal ("10975.368"),
            asesor:{
                nombre:"Javier Dandy",
                sitM:"Se espera una gran baja en el precio de los bonos",
                recom:"Vender bonos"
            },
            operacion:{
                cat:"Bono",
                fdo:new Date(2018,12,10,0,0),
                importe: NumberDecimal ("9521.9994"),
                tipo:"Vender",
                asesor: "Javier Dandy"
            },
},
{
            categoria:"Bono",
            fdc:new Date(2017,12,23,0,0),
            iin: NumberDecimal ("15000.8756"),
            cantidad:20,
            precioac: NumberDecimal ("14895.275"),
            asesor:{
                nombre:"Julio Rofri",
                sitM:"Se espera una gran baja en el precio de los bonos",
                recom:"Vender bonos"
            },
            operacion:{
                cat:"Bono",
                fdo:new Date(2018,03,17,0,0),
                importe: NumberDecimal ("1311.0224"),
                tipo:"Vender",
                asesor: "Julio Rofri"
            },
},
{
            categoria:"Accion",
            fdc:new Date(2019,05,04,0,0),
            iin: NumberDecimal ("6523.789"),
            cantidad:7,
            precioac: NumberDecimal ("7823.6890"),
            asesor:{
                nombre:"Carlos Solange",
                sitM:"Suba de las acciones",
                recom:"Comprar mas acciones"
            },
            operacion:{
                cat:"Accion",
                fdo:new Date(2019,07,5,0,0),
                importe: NumberDecimal ("7825.6899"),
                tipo:"Compra",
                asesor: "Carlos Solange"
            },  
},
{
            categoria:"Accion",
            fdc:new Date(2018,10,21,0,0),
            iin: NumberDecimal ("20658.3689"),
            cantidad:29,
            precioac: NumberDecimal ("25875.4412"),
            asesor:{
                nombre:"Carlos Solange",
                sitM:"Suba de las acciones",
                recom:"Comprar mas acciones"
            },
            operacion:{
                cat:"Accion",
                fdo:new Date(2019,01,15,0,0),
                importe: NumberDecimal ("25995.0254"),
                tipo:"Compra",
                asesor: "Carlos Solange"
            },
},
{
            fdc:new Date(2019,04,03,0,0),
            iin: NumberDecimal("7569.214"),
            cantidad:13,
            precioac: NumberDecimal ("7789.5609"),
            asesor:{
                nombre:"Maria Walmart",
                sitM:"Inestabilidad",
                recom:"Comprar con cautela",
},
            operacion:{
                cat:"Letra",
                fdo:new Date(2019,10,5,0,0),
                importe: NumberDecimal ("8225.1233"),
                tipo:"Compra",
                asesor: "Maria Walmart"
            },  
},
{
            fdc:new Date(2018,07,10,0,0),
            iin: NumberDecimal("10699.994"),
            cantidad:16,
            precioac: NumberDecimal ("11257.214"),
            asesor:{
                nombre:"Mariano Martins",
                sitM:"Inestabilidad",
                recom:"Comprar con cautela",
},
            operacion:{
                cat:"Letra",
                fdo:new Date(2018,12,22,0,0),
                importe: NumberDecimal ("9225.0323"),
                tipo:"Compra",
                asesor: "Mariano Martins"
            },  
},
{
            fdc:new Date(2018,01,18,0,0),
            iin: NumberDecimal("16377.005"),
            cantidad:25,
            precioac: NumberDecimal ("18789.636"),
            asesor:{
                nombre:"Mariano Martins",
                sitM:"Inestabilidad",
                recom:"Comprar con cautela",
},
            operacion:{
                cat:"Letra",
                fdo:new Date(2018,11,20,0,0),
                importe: NumberDecimal ("1025.1023"),
                tipo:"Compra",
                asesor: "Mariano Martins"
            },  
},];
db.inversores.insert(inversiones);


//Inversion con mayor cantidad de operaciones

db.inversores.aggregate({$unwind:"$operacion"},{$group:{_id:'$_id',count:{$sum:1}}},{$sort: {count :-1}})

//Inversion que tuvo mayor ganancia un mes desues de una fecha determinada

db.inversores.aggregate({$project:{_id:1,total:{$subtract : ["$precioac","$iin"]}}}, {$sort: {total : -1}})

//Asesor con mas recomendaciones

db.inversores.aggregate({$unwind:"$operacion"},{$group:{ _id:'$asesor',count:{$sum:1}}},{$sort: {count :-1}})

//Asesor con mas recomendaciones para una inversion determinada

//Esta consulta funciona, pero dado a que no estan creadas las ObjectId de las colecciones, si no se comenta no se puede levantar el script. Nuestra recomendacion es hacer un db.inversores.find() y probar la consulta con cualquier ObjectID de las Inversiones disponibles. Disculpe las molestias

//En el campo de $match, donde esta la condicion _id: ObjectId(), se inserta el ObjectId que quiera probar

//db.inversores.aggregate({$match: {_id: ObjectId("5d9cc9da8c5f451ee46433bc")}},{$unwind:"$operacion"},{$group:{ _id:'$asesor',count:{$sum:1}}},{$sort: {count :-1}});


When I load it, it seems to be working fine, as it returns "true" and it shows my collection when I do db.getCollectionNames(). However, if I do a db.[INSERT_DB_NAME].find(), nothing is returned. I tested this on 2 different Computers, running Ubuntu 18.04.

Sorry if this is a dumb question, but I'm kinda lost.

4
  • I copied this script vebatim and then issued db.inversores.find() from the shell and got all 9 items. Commented Nov 24, 2019 at 14:10
  • Odd. maybe MongoDB installed with errors? At least I know the script works, thanks Commented Nov 24, 2019 at 17:21
  • 1
    Are you absolutely sure that the call to db.[INSERT_DB_NAME].find() is set up properly? And that you are issuing it from the proper DB? How are you loading the script? At the shell, try typing use TPMongo1028257 followed by db.inversores.find() Commented Nov 24, 2019 at 17:48
  • That did the trick! Thanks Buzz! Commented Nov 25, 2019 at 18:00

1 Answer 1

1

Turns out, I never issued the command to the new connection I just made. Silly me.

After doing use TPMongo1028257 it worked just fine

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.