angularjs - Aggregation does not working in Mongoose with Match and Group -
i'm coding application using angularjs, nodejs , mongoose. now, i'm doing chart , need return data passsing parameters. aggregation not work. have query working in mongodb:
var user = db.getcollection('users').find( {profile: "supervisor"},{_id: 1}).map(function(u){return u._id;} ) db.getcollection("visitas").aggregate( { '$match': { createdat: { '$gte': isodate('2015-01-10t00:00:00.000z'), '$lt': isodate('2015-07-01t00:00:00.000z') } } }, { '$group': { _id: { usuario: user, dia: [object] }, visitas: { '$sum': 1 } } } )
when try run same query in application using mongoose, query not work. problem i've seen comma between match , group disapearing. when try search group or match, query works. can me? application code:
.then(function (listausuarios){ var argsvisit; console.log({ $match : { "createdat" : { $gte: "2015-01-10t00:00:00.000z", $lt: "2015-07-01t00:00:00.000z" }} }, { $group : { _id : { "usuario" : listausuarios , "dia" : { $dayofmonth : "$createdat" } }, "visitas" : { $sum : 1 } } }); visita.aggregate({ $match : { "createdat" : { $gte: "2015-01-10t00:00:00.000z", $lt: "2015-07-01t00:00:00.000z" }} }, { $group : { _id : { "usuario" : listausuarios , "dia" : { $dayofmonth : "$createdat" } }, "visitas" : { $sum : 1 } } }) .exec(function (err, results) { if (err) { console.log("erro!!!"); deferreddados.reject({ error: 500, message: 'erro buscando visita, erro: '+err }); } else { if (!results || results.length == 0) { console.log("sem retorno!!!"); deferreddados.resolve( [0,0,0,0,0,0,0] ); //tudo zerado poruqe não há retorno. //deferreddados.resolve(0); deferreddados.reject({ error: 500, message: 'erro buscando visitas, não achou resultados' }); } else { console.log("dados"+results); deferreddados.resolve( results ); //deferreddados.resolve( results.data] ); } } }); return deferreddados.promise; })
solved!!!
a member of team found error! \o/ maybe can have same problem.
to solve, have convert date, in string, date type.
the solved code below:
.then(function (listausuarios){ var argsvisit; if (tipovisita == 'n'){ argsvisit = {'user' : { "$in" :listausuarios }, 'tipo' : { "$in" : ["2001","2002"] }, "createdat": {"$gte": datainicial, "$lt": datafinal }}; } else if (tipovisita == 'r'){ argsvisit = {'user' : { "$in" :listausuarios }, 'tipo' : { "$in" : ["2005"] }, "createdat": {"$gte": datainicial, "$lt": datafinal }}; } else if (tipovisita == 'i'){ argsvisit = {'user' : { "$in" :listausuarios }, 'tipo' : { "$in" : ["2003"] }, "createdat": {"$gte": datainicial, "$lt": datafinal }}; } var initdate = new date('2015-04-02t00:00:00.000z'); var enddate = new date('2015-05-09t00:00:00.000z'); argsmatch = [{ $match : { "createdat" : { $gte: initdate, $lt: enddate }} }, { $group : { _id : { "usuario" : listausuarios , "dia" : { $dayofmonth : "$createdat" } }, "visitas" : {$sum : { $cond: [ {$or : [ { $eq: [ "$tipo", "2001"] },{ $eq: [ "$tipo","2002"] }] },1,0 ]}}} }] visita.aggregate(argsmatch) .exec(function (err, results) { if (err) { console.log("erro!!!"); deferreddados.reject({ error: 500, message: 'erro buscando visita, erro: '+err }); } else { if (!results || results.length == 0) { console.log("sem retorno!!!"); deferreddados.resolve( [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] ); //tudo zerado poruqe não há retorno. //deferreddados.resolve(0); deferreddados.reject({ error: 500, message: 'erro buscando visitas, não achou resultados' }); } else { deferreddados.resolve( results ); } } }); return deferreddados.promise; }) .then(function(dados) { res.json(dados); }) .catch(function(fallback) { console.log('erro no retorno dos usuarios!!!'); console.log(fallback); });
i hope can someone.
Comments
Post a Comment