I have a collection with roughly the following structure:
{
"_id"
"status"
"bids" [
{
"user",
"amount",
"placed"
},
{
"user",
"amount",
"placed"
}
],
"closingAt",
"createdAt"
}
I want to retrieve a list of latest bids from this collection. The bids for each auction object is stored under the array ābidsā.
By using aggregate, I am able to get my desired result in Mongo Shell
db.objects.aggregate({ $unwind: '$bids' }, { $project: { bids: "$bids", _id: 0 } }, { $sort : { "bids.placed" : -1 } });
gives
{ "bids" : { "user" : "j3d8a58AQwngD4gEL", "amount" : 300, "placed" : ISODate("2017-10-09T13:15:00Z") } }
{ "bids" : { "user" : "SH5DqfnH5gSYTTHHa", "amount" : 200, "placed" : ISODate("2017-10-09T13:10:00Z") } }
{ "bids" : { "user" : "j3d8a58AQwngD4gEL", "amount" : 300, "placed" : ISODate("2017-10-09T13:05:00Z") } }
{ "bids" : { "user" : "SH5DqfnH5gSYTTHHa", "amount" : 200, "placed" : ISODate("2017-10-09T13:00:00Z") } }
{ "bids" : { "user" : "j3d8a58AQwngD4gEL", "amount" : 300, "placed" : ISODate("2017-10-09T12:55:00Z") } }
{ "bids" : { "user" : "SH5DqfnH5gSYTTHHa", "amount" : 200, "placed" : ISODate("2017-10-09T12:50:00Z") } }
{ "bids" : { "user" : "j3d8a58AQwngD4gEL", "amount" : 300, "placed" : ISODate("2017-10-09T12:45:00Z") } }
{ "bids" : { "user" : "SH5DqfnH5gSYTTHHa", "amount" : 200, "placed" : ISODate("2017-10-09T12:40:00Z") } }
{ "bids" : { "user" : "SH5DqfnH5gSYTTHHa", "amount" : 200, "placed" : ISODate("2017-10-09T12:35:00Z") } }
{ "bids" : { "user" : "j3d8a58AQwngD4gEL", "amount" : 300, "placed" : ISODate("2017-10-09T12:30:00Z") } }
{ "bids" : { "user" : "j3d8a58AQwngD4gEL", "amount" : 300, "placed" : ISODate("2017-10-09T12:25:00Z") } }
{ "bids" : { "user" : "SH5DqfnH5gSYTTHHa", "amount" : 200, "placed" : ISODate("2017-10-09T12:20:00Z") } }
{ "bids" : { "user" : "j3d8a58AQwngD4gEL", "amount" : 300, "placed" : ISODate("2017-10-09T12:15:00Z") } }
{ "bids" : { "user" : "SH5DqfnH5gSYTTHHa", "amount" : 200, "placed" : ISODate("2017-10-09T12:10:00Z") } }
{ "bids" : { "user" : "j3d8a58AQwngD4gEL", "amount" : 300, "placed" : ISODate("2017-10-09T12:05:00Z") } }
{ "bids" : { "user" : "SH5DqfnH5gSYTTHHa", "amount" : 200, "placed" : ISODate("2017-10-09T12:00:00Z") } }
Using the jcbernack:reactive-aggregate and your package gives me
a) with _ids - { $project: { bids: ā$bidsā } }
{ "_id" : "RN9Zejme54AhN6EZg", "bids" : { "user" : "SH5DqfnH5gSYTTHHa", "amount" : 200, "placed" : ISODate("2017-10-09T13:10:00Z") } }
{ "_id" : "WDnmJ3x6EsX4dboWy", "bids" : { "user" : "SH5DqfnH5gSYTTHHa", "amount" : 200, "placed" : ISODate("2017-10-09T13:00:00Z") } }
{ "_id" : "RrpwdyN5YzvHHKqBn", "bids" : { "user" : "SH5DqfnH5gSYTTHHa", "amount" : 200, "placed" : ISODate("2017-10-09T12:50:00Z") } }
{ "_id" : "pWi9yKEAkrChQqv84", "bids" : { "user" : "SH5DqfnH5gSYTTHHa", "amount" : 200, "placed" : ISODate("2017-10-09T12:40:00Z") } }
{ "_id" : "YAcsjw4Hm32rPvz77", "bids" : { "user" : "j3d8a58AQwngD4gEL", "amount" : 300, "placed" : ISODate("2017-10-09T12:30:00Z") } }
{ "_id" : "kvPHx2TCwoo3M7M3o", "bids" : { "user" : "SH5DqfnH5gSYTTHHa", "amount" : 200, "placed" : ISODate("2017-10-09T12:20:00Z") } }
{ "_id" : "D53zkHbv9YNY8YXfi", "bids" : { "user" : "SH5DqfnH5gSYTTHHa", "amount" : 200, "placed" : ISODate("2017-10-09T12:10:00Z") } }
{ "_id" : "Jh6chaQCh9woLiLDk", "bids" : { "user" : "SH5DqfnH5gSYTTHHa", "amount" : 200, "placed" : ISODate("2017-10-09T12:00:00Z") } }
b) excluding the _ids - { $project: { bids: ā$bidsā, _id: 0 }
Gives an error
Uncaught Error: Expected to find a document to change
at Object.update (http://localhost:3000/packages/mongo.js?hash=191d09671a8d33340d4acaaa8c6015f73cc58072:245:27)
at Object.store.(anonymous function) [as update] (http://localhost:3000/packages/ddp-client.js?hash=8b46f76bbf82e182fe81190d0eea965a685cbd77:3732:60)
at http://localhost:3000/packages/ddp-client.js?hash=8b46f76bbf82e182fe81190d0eea965a685cbd77:4539:19
at Array.forEach (<anonymous>)
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?hash=cde485f60699ff9aced3305f70189e39c665183c:149:11)
at http://localhost:3000/packages/ddp-client.js?hash=8b46f76bbf82e182fe81190d0eea965a685cbd77:4538:13
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?hash=cde485f60699ff9aced3305f70189e39c665183c:157:22)
at Connection._performWrites (http://localhost:3000/packages/ddp-client.js?hash=8b46f76bbf82e182fe81190d0eea965a685cbd77:4534:9)
at Connection._flushBufferedWrites (http://localhost:3000/packages/ddp-client.js?hash=8b46f76bbf82e182fe81190d0eea965a685cbd77:4521:10)
at Connection._livedata_data (http://localhost:3000/packages/ddp-client.js?hash=8b46f76bbf82e182fe81190d0eea965a685cbd77:4487:12)
This line gives me the error:
} else if (msg.msg === 'changed') {
if (!doc)
throw new Error("Expected to find a document to change");
And returns only one array in the result
{ "bids" : { "user" : "j3d8a58AQwngD4gEL", "amount" : 300, "placed" : ISODate("2017-10-09T13:15:00Z") } }