Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
¿Cómo puedo agregar un nuevo campo al resultado agregado de Mongo?

Tengo la siguiente consulta agregada de mongodb, ese grupo por fecha (1-05-2017 a 1-13-2017)

 CampaignActivity.aggregate([ { "$match": { "$and":[{updatedAt: {$lt: new Date('1-13-2017')}},{updatedAt: {$gte: new Date('1-05-2017')}}] } }, { "$project" : { _id : 0, "datePartDay" : {"$concat" : [ {"$substr" : [{"$dayOfMonth" : "$updatedAt"}, 0, 2]}, "-", {"$substr" : [{"$month" : "$updatedAt"}, 0, 2]}, "-", {"$substr" : [{"$year" : "$updatedAt"}, 0, 4]} ] }, "isdelivered": {"$cond": { if: { $eq: [ "$delivered", true ] }, then: 1, else: 0 }}, "isclicked": {"$cond": { if: { $eq: [ "$clicked", true ] }, then: 1, else: 0 }}, "isunsubscribe": {"$cond": { if: { $eq: [ "$unsubscribed", true ] }, then: 1, else: 0 }} } }, { "$group" : { "_id" : "$datePartDay", "delivered" : { "$sum" :'$isdelivered' }, "clicked" : { "$sum" :'$isclicked' }, "unsubscribed" : { "$sum" :'$isunsubscribe' } } } ],function (err, result) { if (err) { console.log(err); } else { console.log(result); } });

resultado agregado:

 [ { "_id": "12-1-2017", "delivered": 0, "clicked": 1, "unsubscribed": 1 }, { "_id": "11-1-2017", "delivered": 2, "clicked": 1, "unsubscribed": 0 } ]
  • ¿Es posible agregar nuevos campos para que no exista en la colección algo como "fecha"?

  • ¿Es posible agregar campos de fecha faltantes con datos ficticios?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

La etapa $project que tienes es redundante. Puede mover fácilmente toda esa lógica a la etapa de $group y terminar con la etapa de $project para reformatear su respuesta.

Algo como esto a continuación.

 [{ "$match": { "$and": [{ updatedAt: { $lt: new Date('1-13-2017') } }, { updatedAt: { $gte: new Date('1-05-2017') } }] } }, { "$group": { "_id": { $dateToString: { format: "%m-%d-%Y", date: "$updatedAt" } }, "delivered": { $sum: { "$cond": { if: { $eq: ["$delivered", true] }, then: 1, else: 0 } } }, "clicked": { $sum: { "$cond": { if: { $eq: ["$clicked", true] }, then: 1, else: 0 } } }, "unsubscribed": { $sum: { "$cond": { if: { $eq: ["$unsubscribed", true] }, then: 1, else: 0 } } } } }, { "$project": { "_id": 0, "date": "$_id", "delivered": 1, "clicked": 1, "unsubscribed": 1 } }]
over 4 years ago · Santiago Trujillo Report

0

arr.map es bueno, pero hay una mejor manera de lograr el mismo resultado a través de $addFields, simplemente inserte el código a continuación justo después de $group stage

 { $group : { _id : "$datePartDay", delivered : { "$sum" :'$isdelivered' }, clicked : { "$sum" :'$isclicked' }, unsubscribed : { "$sum" :'$isunsubscribe' } } } { $addFields: { date: new Date() } }

Y recibirás:

 [ { "_id": "12-1-2017", "delivered": 0, "clicked": 1, "unsubscribed": 1 "date": 2018-02-17T06:12:53.538Z }, { "_id": "11-1-2017", "delivered": 2, "clicked": 1, "unsubscribed": 0 "date": 2018-02-17T06:12:53.538Z } ]
over 4 years ago · Santiago Trujillo Report

0

Es posible.

Te mostraré mi manera de hacerlo:

 function (err, res) { if (err) { console.log(err); } else { console.log(res); var result = res.map(function(item) { var jsonValue = {}; jsonValue["_id"] = item._id; jsonValue["delivered"]= item.delivered; jsonValue["clicked"] = item.clicked; // You can add what you want jsonValue["date"] = new Date(); return jsonValue; } } }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!