Update document after aggregation operation

2,044 views
Skip to first unread message

Thales Fernando

unread,
Mar 24, 2014, 8:05:21 PM3/24/14
to mongod...@googlegroups.com
In documentation, Mongo tells that the result of an aggregation operation is plain JavaScript. How update a document after an aggregation operation?

Example:
User.aggregate({...}, function (errFinding, doc) {
doc.name = 'John';
doc.save({...});
});

Thales Fernando

unread,
Mar 24, 2014, 10:02:22 PM3/24/14
to mongod...@googlegroups.com
Real example:

// With this model in DB;

{ _id: 52fbc7f54024b87218000003,
  base: 'gru',
  company: 'American Airlines’,
  pin: ‘1111',
  updatedAt: Mon Mar 24 2014 22:23:20 GMT-0300 (BRT),
  createdAt: Wed Feb 12 2014 16:13:53 GMT-0300 (BRT),
  offers: [{
_id: 532cab1d592bd7c407000005,
period: 2,
dateBegin: Mon Jan 20 2014 00:00:00 GMT-0300 (BRT),
dateEnd: Thu Jan 30 2014 00:00:00 GMT-0300 (BRT),
description: 'Airbus A380',
destinationAirportName: 'John F. Kennedy International Airport',
destinationIATACode: 'JFK',
fromAirportName: 'Pluncton',
fromIATACode: 'PLC',
updatedAt: Fri Mar 21 2014 18:11:57 GMT-0300 (BRT),
createdAt: Fri Mar 21 2014 18:11:50 GMT-0300 (BRT),
status: 'active',
bids: [{
_id: 5330da8834a86cb618000006,
from: 'john doe',
description: 'i\'m a test!',
date: Mon Mar 24 2014 22:23:20 GMT-0300 (BRT)
}]
  }],
}

// With this search
User.aggregate(
{$match: {
'_id': mongoose.Types.ObjectId('52fbc7f54024b87218000003'),
'offers._id' : mongoose.Types.ObjectId('532cab1d592bd7c407000005'),
}},
{$unwind: '$offers'},
{$project: {
'_id': 0,
'offerID': '$offers._id',
updatedAt: '$offers.updatedAt',
dateEnd: '$offers.dateEnd',
dateBegin: '$offers.dateBegin',
description: '$offers.description',
destinationAirportName: '$offers.destinationAirportName',
destinationIATACode: '$offers.destinationIATACode',
fromAirportName: '$offers.fromAirportName',
fromIATACode: '$offers.fromIATACode',
createdAt: '$offers.createdAt',
status: '$offers.status',
period: '$offers.period'
}}, function (errFinding, doc) {
// if occour some error or offers not found, respond
if (errFinding) {console.log(errFinding);}
if (!doc || doc.length === 0) {console.log("There's doc");}

console.log(doc);
/**
* Output:
[{
offerID: 532cab1d592bd7c407000005,
updatedAt: Fri Mar 21 2014 18:11:57 GMT-0300 (BRT),
dateEnd: Thu Jan 30 2014 00:00:00 GMT-0300 (BRT),
dateBegin: Mon Jan 20 2014 00:00:00 GMT-0300 (BRT),
description: 'Airbus A380',
destinationAirportName: 'John F. Kennedy International Airport',
destinationIATACode: 'JFK',
fromAirportName: 'Pluncton',
fromIATACode: 'PLC',
createdAt: Fri Mar 21 2014 18:11:50 GMT-0300 (BRT),
status: 'active',
period: 2
}]
*/

// IT'S POSSIBLE? UPDATE? NO...
doc.period = 10;
doc.save(function (errSaving) {
console.log(errSaving || 'Oh yeah!');
});

/**
* Output:
    Connection error: TypeError: Object [object Object] has no method 'save'
*/
});

Asya Kamsky

unread,
Mar 25, 2014, 1:24:49 PM3/25/14
to mongodb-user
Look, I think this clearly demonstrates why you need to change your schema.

If you embed offers inside User then you have to use aggregations to do what *should* be simple queries.

When making simple changes to a document, you should be able to use "update".  When doing regular queries you should be able to use "find" - if you need "aggregate" for something other than actual aggregations then the schema doesn't match your requirements.

Asya



--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/01af7956-eebe-4236-84da-ee7aba7087b3%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Thales Fernando

unread,
Mar 25, 2014, 3:53:31 PM3/25/14
to mongod...@googlegroups.com
Very thanks! I’m switching everything to refs, not dbrefs ;)

Cheers!

Thales Fernando

You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/hL_d7G1OLMg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.

To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
Reply all
Reply to author
Forward
0 new messages