How can I Pivot on MongoDB - mongodb

Given the documents:
{
"_id" : "8843c0c0-846f-44ae-9b69-a124dd28f4db",
"purchaseOrderData" : {
"purchaseOrderId" : "WI9ZF"
}
}
{
"_id" : "c3fb80fc-75c0-4259-9d5a-1bc6de1ce7d7",
"purchaseOrderData" : {
"purchaseOrderId" : "WJ0DD"
}
}
{
"_id" : "2ae496e6-28f9-4baa-b952-2054a505f658",
"purchaseOrderData" : {
"purchaseOrderId" : "WI8PP"
}
}
{
"_id" : "421ccbed-0824-443f-bf42-3f0049f46c71",
"purchaseOrderData" : {
"purchaseOrderId" : "WI9WH"
}
}
{
"_id" : "5481b1ef-5f0f-4ba1-8210-d704e9650af4",
"purchaseOrderData" : {
"purchaseOrderId" : "WI9ZH"
}
}
{
"_id" : "1640a27a-6577-4240-8aaa-4c4b1334fd6d",
"purchaseOrderData" : {
"purchaseOrderId" : "WJ0DD"
}
}
{
"_id" : "bd95d801-da2d-4556-a223-dcff30b6ab9d",
"purchaseOrderData" : {}
}
{
"_id" : "4880f816-41e7-43bc-bed4-e8574aa9c045",
"purchaseOrderData" : {
"purchaseOrderId" : "WI9LA"
}
}
{
"_id" : "fe651764-aeb4-460e-89fa-474fcec33f19",
"purchaseOrderData" : {}
}
{
"_id" : "4d73431c-85dc-479a-8739-b314d6cd9636",
"purchaseOrderData" : {
"purchaseOrderId" : "WI9LA"
}
}
i want a result like:
{
"purchaseOrderData.purchaseOrderId":["_id1","_id2"]
}
ex:
[{
"WI9ZF":["8843c0c0-846f-44ae-9b69-a124dd28f4db"]
},{
"WI9WH":["421ccbed-0824-443f-bf42-3f0049f46c71","5481b1ef-5f0f-4ba1-8210-d704e9650af4"]
}]

there was a few question similar to yours befere [1] [2]
using aggregation framework you can achieve a close document shape, but there will be a need to reshape this on app server side.
db.leonardo.aggregate([
{
$group:{
"_id":"$purchaseOrderData.purchaseOrderId",
"data":{$push:"$_id"}
}
}])
will give this output for given dataset:
{ "_id" : "WI9WH", "data" : [ "421ccbed-0824-443f-bf42-3f0049f46c71" ] }
{ "_id" : "WI9ZF", "data" : [ "8843c0c0-846f-44ae-9b69-a124dd28f4db" ] }
{
"_id" : "WJ0DD",
"data" : [
"c3fb80fc-75c0-4259-9d5a-1bc6de1ce7d7",
"1640a27a-6577-4240-8aaa-4c4b1334fd6d"
]
}
{ "_id" : "WI8PP", "data" : [ "2ae496e6-28f9-4baa-b952-2054a505f658" ] }
{ "_id" : "WI9ZH", "data" : [ "5481b1ef-5f0f-4ba1-8210-d704e9650af4" ] }
{ "_id" : null, "data" : [ "bd95d801-da2d-4556-a223-dcff30b6ab9d" ] }
{ "_id" : "WI9LA", "data" : [ "4880f816-41e7-43bc-bed4-e8574aa9c045" ] }

Related

How to write mongo query

How I can get the total number of seats available for a particular movie (seats present in all the theatres for that movie) from the mongodb schema below.
I need to write a mongo query to get the results
{
"_id" : ObjectId("5d637b5ce27c7d60e5c42ae7"),
"name" : "Bangalore",
"movies" : [
{
"name" : "KGF",
"theatres" : [
{
"name" : "PVR",
"seats" : 45
},
{
"name" : "IMAX",
"seats" : 46
}
]
},
{
"name" : "Avengers",
"theatres" : [
{
"name" : "IMAX",
"seats" : 50
}
]
}
],
"_class" : "com.BMS_mongo.ZZ_BMS_mongo_demo.Entity.CityInfo"
}
I have written this code :
db.cities.aggregate( [
{ "$unwind" : "$movies" }, { "$unwind" : "$theatres" } ,
{ "$group" : { _id : "$movies.theatre`enter code here`s.seats" ,
total : { "$sum" : "$seats" } }
}
] )
My schema:
The following query can get us the expected output:
db.collection.aggregate([
{
$unwind:"$movies"
},
{
$unwind:"$movies.theatres"
},
{
$group:{
"_id":"$movies.name",
"movie":{
$first:"$movies.name"
},
"totalSeats":{
$sum:"$movies.theatres.seats"
}
}
},
{
$project:{
"_id":0
}
}
]).pretty()
Data set:
{
"_id" : ObjectId("5d637b5ce27c7d60e5c42ae7"),
"name" : "Bangalore",
"movies" : [
{
"name" : "KGF",
"theatres" : [
{
"name" : "PVR",
"seats" : 45
},
{
"name" : "IMAX",
"seats" : 46
}
]
},
{
"name" : "Avengers",
"theatres" : [
{
"name" : "IMAX",
"seats" : 50
}
]
}
],
"_class" : "com.BMS_mongo.ZZ_BMS_mongo_demo.Entity.CityInfo"
}
Output:
{ "movie" : "Avengers", "totalSeats" : 50 }
{ "movie" : "KGF", "totalSeats" : 91 }
Query:
db.movie.aggregate([{ $unwind: { path: "$movies",} },
{ $unwind: { path: "$movies.theatres",} },
{ $group: { _id: "$movies.name", "moviename": { $first: "$movies.name" },
"totalSeats": { $sum: "$movies.theatres.seats" }} }])
I got the answer using this query ...
db.cities.aggregate( [
{ "$match" : { "name" : "Bangalore" } },
{ "$unwind" : "$movies" } ,
{ "$match" : {"movies.name" : "KGF"} },
{ "$unwind" : "$theatres" },
{ "$group" : { _id : "$movies.name", total : { "$sum" : "$movies.theatres.seats"
} } }
] )

MongoDB slow facet query using multiple group by

I am working on a project where we will have almost 5 million documents in a collection. And each document's size will be around 18571 bytes having 120 to 150 fields.
I have to return my response in less than 1 second. And mongo query will perform almost 10 to 15 group by in faceted query on maximum 3,00,000 documents.
This is my first time handling this much of data where I have to return responses in real time.
I have implemented indexes and they reduced response time to 5 to 6 seconds but I still need it in less then 1 second.
Below is sample query:
db.sample.aggregation(
"$match":{
"$and":[
{"is_new": <true/false>},
{"brand":<some-brand>},
{"year":{"$gte":<some-year>,"$lte":<some-year>}},
{"seller_id":{"$in":[<array-of-seller-ids-may-have-40,000-seller-ids>]}}
]
},
{
"$facet":{
"data":[{
"$project":{
"_id":"_id",
"brand":"$brand_name",
"model":"$model_name",
<will have almost 20 keys with lookup>
}
}],
"count":[{"$group":{"_id":"$_id"}},{"$count":"vin_count"}],
"price":[{"$bucketAuto":{"groupBy":"$price", "buckets":1}}],
<will have 12-15 group by>
}
}
)
Below is sample document:
{
"_id" : "KNDMC5C11J6394584",
"brand_id" : 22,
"brand_name" : "XYZ",
"abc_id" : 1234567890,
"city" : "Gurgaon, IN",
"fluctuation" : 18,
"created_at" : ISODate("2018-08-17T06:08:12.940Z"),
"release_data" : "2018-06-29",
"seller_name" : "Seller name",
"seller_price" : 34890,
"seller_rating" : 4,
"seller_zip" : "12550",
"feature1" : "ABC",
"feature2" : 3300,
"feature3" : "AB",
"expected_price" : -1,
"exterior_color" : "Unknown",
"registered_dealer" : true,
"registered_brand" : "ABC",
"fluctuation_rate" : 20.700000000000003,
"fluctuation_type" : 2,
"fluc_type_name" : "Something",
"has_patents" : false,
"tested_frequency" : 24,
"interior_color" : "---",
"is_certified" : false,
"is_certified_iso" : false,
"is_featured" : false,
"is_new" : true,
"is_certified_bhel" : false,
"location" : {
"type" : "Point",
"coordinates" : [
-24.08180236816406,
31.507198333740234
]
},
"max_input" : 8,
"feature4" : 3,
"feature5" : 206,
"feature6" : "Something",
"monthly_payment" : 649,
"msrp" : 34890,
"feature7" : false,
"seller_id" : 123567890,
"product_family_name" : "abc",
"product_id" : 15,
"product_name" : "Something",
"reflection" : "Something",
"fluc_id" : 2312,
"fluc_name" : "something something (abc) ac",
"updated_at" : ISODate("2018-09-11T17:59:36.889Z"),
"product_damage_category" : "None",
"year" : 2018,
"damage_check" : "-",
"team_size" : "-",
"Technology" : {
"camera_unit" : true
}
}
Below is the explain output
{
"stages" : [
{
"$cursor" : {
"query" : {
"$and" : [
{
"is_new" : true
},
{
"year" : {
"$gte" : 2018,
"$lte" : 2018
}
},
{
"sp_id" : {
"$in" : [<list of 40,000 seller ids>]
}
}
]
},
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test_collection.col",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"is_new" : {
"$eq" : true
}
},
{
"year" : {
"$lte" : 2018
}
},
{
"year" : {
"$gte" : 2018
}
},
{
"sp_id" : {
"$in" : [<list of 40,000 seller ids>]
}
}
]
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"is_new" : 1,
"year" : 1,
"sp_id" : 1
},
"indexName" : "is_new_1_year_1_sp_id_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"is_new" : [ ],
"year" : [ ],
"sp_id" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"is_new" : [
"[true, true]"
],
"year" : [
"[2018.0, 2018.0]"
],
"sp_id" : [
"[47590.0, 47590.0]",
"[48333.0, 48333.0]",
"[51333.0, 51333.0]",
<range of 40,000 seller_ids>
]
}
}
},
"rejectedPlans" : [ ]
}
}
},
{
"$facet" : {
"data" : [
{
"$project" : {
"_id" : "$_id",
"brand_name" : "$brand_name",
"feature1" : "$feature1",
"feature2" : "$feature2",
"feature3" : "$feature3",
"feature4" : "$feature4",
"feature5" : "$feature5",
"feature6" : "$feature6",
"feature7" : "$feature7",
"feature8" : "$feature8",
"feature9" : "$feature9",
"feature10" : "$feature10",
"feature11" : "$feature11",
"feature12" : "$feature12",
"feature13" : "$feature13",
"feature14" : "$feature14",
"feature15" : "$feature15",
"feature16" : "$feature16",
"feature17" : "$feature17",
"feature18" : "$feature18",
"feature19" : "$feature19",
"feature20" : "$feature20"
}
}
],
"count" : [
{
"$group" : {
"_id" : "$_id"
}
},
{
"$group" : {
"_id" : {
"$const" : null
},
"count" : {
"$sum" : {
"$const" : 1
}
}
}
},
{
"$project" : {
"_id" : false,
"count" : true
}
}
],
"feature1" : [
{
"$match" : {
"feature1" : {
"$exists" : true
}
}
},
{
"$group" : {
"_id" : "$feature1",
"name" : {
"$first" : "$feature1"
},
"count" : {
"$sum" : {
"$const" : 1
}
}
}
},
{
"$sort" : {
"sortKey" : {
"count" : -1
}
}
}
],
"feature2" : [
{
"$match" : {
"feature2" : {
"$exists" : true
}
}
},
{
"$group" : {
"_id" : "$feature2",
"name" : {
"$first" : "$feature2"
},
"count" : {
"$sum" : {
"$const" : 1
}
}
}
},
{
"$sort" : {
"sortKey" : {
"count" : -1
}
}
}
],
"feature3" : [
{
"$match" : {
"feature3" : {
"$exists" : true
}
}
},
{
"$group" : {
"_id" : "$feature3",
"name" : {
"$first" : "$feature3"
},
"count" : {
"$sum" : {
"$const" : 1
}
}
}
},
{
"$sort" : {
"sortKey" : {
"count" : -1
}
}
}
],
"feature4" : [
{
"$match" : {
"feature4" : {
"$exists" : true
}
}
},
{
"$group" : {
"_id" : "$feature4",
"name" : {
"$first" : "$feature4"
},
"count" : {
"$sum" : {
"$const" : 1
}
}
}
},
{
"$sort" : {
"sortKey" : {
"count" : -1
}
}
}
],
"feature5" : [
{
"$match" : {
"feature5" : {
"$exists" : true
}
}
},
{
"$group" : {
"_id" : "$feature5",
"name" : {
"$first" : "$fuel"
},
"count" : {
"$sum" : {
"$const" : 1
}
}
}
},
{
"$sort" : {
"sortKey" : {
"count" : -1
}
}
}
],
"feature6" : [
{
"$match" : {
"feature6" : {
"$exists" : true
}
}
},
{
"$group" : {
"_id" : "$feature6",
"name" : {
"$first" : "$feature6"
},
"count" : {
"$sum" : {
"$const" : 1
}
}
}
},
{
"$sort" : {
"sortKey" : {
"count" : -1
}
}
}
],
"feature7" : [
{
"$match" : {
"feature7" : {
"$exists" : true
}
}
},
{
"$group" : {
"_id" : "$feature7",
"name" : {
"$first" : "$feature7"
},
"count" : {
"$sum" : {
"$const" : 1
}
}
}
},
{
"$sort" : {
"sortKey" : {
"count" : -1
}
}
}
],
"feature8" : [
{
"$match" : {
"feature8" : {
"$exists" : true
}
}
},
{
"$group" : {
"_id" : "$feature8",
"name" : {
"$first" : "$feature8"
},
"count" : {
"$sum" : {
"$const" : 1
}
}
}
},
{
"$sort" : {
"sortKey" : {
"count" : -1
}
}
}
],
"feature9" : [
{
"$match" : {
"feature9" : {
"$exists" : true
}
}
},
{
"$group" : {
"_id" : "$feature9",
"name" : {
"$first" : "$feature9"
},
"count" : {
"$sum" : {
"$const" : 1
}
}
}
},
{
"$sort" : {
"sortKey" : {
"count" : -1
}
}
}
],
"feature10" : [
{
"$match" : {
"feature10" : {
"$exists" : true
}
}
},
{
"$group" : {
"_id" : "$feature10",
"name" : {
"$first" : "$feature10"
},
"count" : {
"$sum" : {
"$const" : 1
}
}
}
},
{
"$sort" : {
"sortKey" : {
"_id" : -1
}
}
}
],
"feature11" : [
{
"$match" : {
"feature11" : {
"$exists" : true
}
}
},
{
"$bucketAuto" : {
"groupBy" : "$feature11",
"buckets" : 1,
"output" : {
"count" : {
"$sum" : {
"$const" : 1
}
}
}
}
}
],
"feature12" : [
{
"$bucketAuto" : {
"groupBy" : "$feature11",
"buckets" : 1,
"output" : {
"count" : {
"$sum" : {
"$const" : 1
}
}
}
}
}
]
}
}
],
"ok" : 1
}
If this information is not complete for the solution. I will provide more.
I am stuck on this from last 1 month.
Any help would be appreciated.

Mongo DB Sorting issue with Ascending order

I have a collection named "formTest123" with following records:
/* 0 */
{
"_id" : ObjectId("5784f5aeef31a98294231459"),
"data" : [
{
"name" : "Amir",
"Other" : [
{
"data" : {
"city" : {
"address" : "pncjj"
}
}
},
{
"data" : {
"state" : {
"address" : "xyz"
}
}
}
]
}
]
}
/* 1 */
{
"_id" : ObjectId("5784f62cef31a9829423145a"),
"data" : [
{
"name" : "Zssmir",
"Other" : [
{
"data" : {
"city" : {
"address" : "bncd"
}
}
},
{
"data" : {
"state" : {
"address" : "gyk"
}
}
}
]
}
]
}
/* 2 */
{
"_id" : ObjectId("5784f636ef31a9829423145b"),
"data" : [
{
"name" : "Cmir",
"Other" : [
{
"data" : {
"city" : {
"address" : "tuhn"
}
}
},
{
"data" : {
"state" : {
"address" : "knm"
}
}
}
]
}
]
}
When I query this collection with:
db.formTest123.find().sort( { "data.Other.data.city.address" : -1})
means in descending order it gives correct output that is :
/* 0 */
{
"_id" : ObjectId("5784f636ef31a9829423145b"),
"data" : [
{
"name" : "Cmir",
"Other" : [
{
"data" : {
"city" : {
"address" : "tuhn"
}
}
},
{
"data" : {
"state" : {
"address" : "knm"
}
}
}
]
}
]
}
/* 1 */
{
"_id" : ObjectId("5784f5aeef31a98294231459"),
"data" : [
{
"name" : "Amir",
"Other" : [
{
"data" : {
"city" : {
"address" : "pncjj"
}
}
},
{
"data" : {
"state" : {
"address" : "xyz"
}
}
}
]
}
]
}
/* 2 */
{
"_id" : ObjectId("5784f62cef31a9829423145a"),
"data" : [
{
"name" : "Zssmir",
"Other" : [
{
"data" : {
"city" : {
"address" : "bncd"
}
}
},
{
"data" : {
"state" : {
"address" : "gyk"
}
}
}
]
}
]
}
But When I query with:
db.formTest123.find().sort( { "data.Other.data.city.address" : 1})
to get in ascending order of "city.address" it gives:
/* 0 */
{
"_id" : ObjectId("5784f5aeef31a98294231459"),
"data" : [
{
"name" : "Amir",
"Other" : [
{
"data" : {
"city" : {
"address" : "pncjj"
}
}
},
{
"data" : {
"state" : {
"address" : "xyz"
}
}
}
]
}
]
}
/* 1 */
{
"_id" : ObjectId("5784f62cef31a9829423145a"),
"data" : [
{
"name" : "Zssmir",
"Other" : [
{
"data" : {
"city" : {
"address" : "bncd"
}
}
},
{
"data" : {
"state" : {
"address" : "gyk"
}
}
}
]
}
]
}
/* 2 */
{
"_id" : ObjectId("5784f636ef31a9829423145b"),
"data" : [
{
"name" : "Cmir",
"Other" : [
{
"data" : {
"city" : {
"address" : "tuhn"
}
}
},
{
"data" : {
"state" : {
"address" : "knm"
}
}
}
]
}
]
}
That is clearly wrong as now records are not sorted in ascending order by "city.address"
Can any one guess what is problem with ascending order?
It is because it is ordering from the "lowest" to the "highest".
Problem is, that undefined and/or null has "lower" value than any existing number.
When you ordering by array, it looks to all documents in that array and takes the minimum value, which is "undefined" in the document, which contains state.
If you for example add "city.address" with high-enough value to all your documents, it will work for ascending. Like this one :
"Other" : [
{
"data" : {
"city" : {
"address" : "tuhn"
}
}
},
{
"data" : {
"state" : {
"address" : "knm"
}
"city" : {
"address" : "zzzzzzzzzzzzzzzzz"
}
}
}
]
Note : For descending, it takes maximum value, therefore it "works", because any city.address override all the null and undefined.
If you need ascending order and dont want to change the data structure, I would suggest to order it descending and then reverse order programically, if it is possible.
Since field "Other" is an array of (sub)documents, we must specify the index of the sub-document.
Based on the schema specified in question, following works.
db.formTest123.find().sort( { "data.Other.0.data.city.address" : 1});
and
db.formTest123.find().sort( { "data.Other.0.data.city.address" : -1});

how to get this query in mongoDB

I have this collection...
> db.banks.find().pretty()
{
"_id" : ObjectId("54f37cbb44aec3b01b7db8f4"),
"name" : "A",
"branches" : [
{
"branch_id" : 8561,
"name" : "X",
},
{
"branch_id" : 8576,
"name" : "Y",
}
]
}
{
"_id" : ObjectId("54f37cbb44aec3b01b7db8f5"),
"name" : "B",
"branches" : [
{
"branch_id" : 3238,
"name" : "Z",
}
]
}
with this command :
db.banks.aggregate({$project{"branches.name":1,"_id":0}});
get this result :
{ "branches" : { { "name" : "X" }, { "name" : "Y" } } }
{ "branches" : { { "name" : "Z" } } }
but; how I get this result?
(In fact, one object and without "branches".)
{{"name" : "X"}, {"name" : "Y"}, {"name" : "Z"}}
very thanks...
One way you could go about this is to do an $unwind first in the aggregation pipeline to get a deconstructed array with a document for each element and then group by the array element $branches.name:
db.banks.aggregate([
{ $unwind: '$branches'},
{
$group: {
_id: {
name: '$branches.name'
}
}
},
{
$project: {
_id: 0,
name: '$_id.name'
}
},
{ $sort : { "name" : 1 } }
])
Outputs:
{
"result" : [
{
"name" : "X"
},
{
"name" : "Y"
},
{
"name" : "Z"
}
],
"ok" : 1
}

Unexpected output from map reduce

I have the following map and reduce functions that should give me the max date from a group.
Map:
function () {
if (this.topic_id != 0) {
emit(this.topic_id, {date_posted : this.date_posted});
}
}
Reduce:
function (key, values) {
var re_date = ISODate('1970-01-01T00:00:00Z');
values.forEach(function (value) {
if (re_date == ISODate('1970-01-01T00:00:00Z')) {
re_date = value.date_posted;
}
if (re_date < value.date_posted) {
re_date = value.date_posted;
}
});
return {date_posted: re_date};
}
At the moment, it is giving the following output:
{ "_id" : "1", "value" : ISODate("2010-06-01T13:36:30Z") }
{ "_id" : "10", "value" : { "date_posted" : ISODate("2010-05-19T21:33:16Z") } }
{ "_id" : "1000", "value" : { "date_posted" : ISODate("2010-07-04T04:09:43Z") } }
{ "_id" : "1004", "value" : ISODate("2010-07-05T01:27:53Z") }
{ "_id" : "1007", "value" : { "date_posted" : ISODate("2010-07-04T23:44:56Z") } }
{ "_id" : "1009", "value" : ISODate("2010-08-12T19:05:35Z") }
{ "_id" : "1012", "value" : ISODate("2010-08-14T19:56:35Z") }
{ "_id" : "1014", "value" : { "date_posted" : ISODate("2010-07-05T07:18:56Z") } }
{ "_id" : "1022", "value" : ISODate("2010-07-06T21:17:57Z") }
{ "_id" : "1024", "value" : { "date_posted" : ISODate("2010-07-06T12:37:28Z") } }
{ "_id" : "1028", "value" : ISODate("2010-07-08T00:24:59Z") }
{ "_id" : "1029", "value" : ISODate("2010-07-26T12:08:34Z") }
{ "_id" : "1042", "value" : { "date_posted" : ISODate("2010-10-01T19:22:29Z") } }
{ "_id" : "1043", "value" : { "date_posted" : ISODate("2010-07-20T01:38:44Z") } }
{ "_id" : "1048", "value" : { "date_posted" : ISODate("2010-07-08T19:29:14Z") } }
{ "_id" : "1049", "value" : ISODate("2010-07-08T11:07:30Z") }
{ "_id" : "105", "value" : ISODate("2010-05-21T01:53:20Z") }
{ "_id" : "1053", "value" : { "date_posted" : ISODate("2010-07-08T17:53:57Z") } }
{ "_id" : "1056", "value" : { "date_posted" : ISODate("2010-07-08T22:35:02Z") } }
{ "_id" : "1060", "value" : { "date_posted" : ISODate("2010-07-09T06:33:57Z") } }
I was expecting that the value field for every line would always have the "date_posted" element but clearly only some have this. Do I have a mistake in my map function?
A sample of the original data:
db.posts.find({"topic_id" : {$in : ["105", "1053", "1000", "1004", "1007"]}}, {"date_posted" : 1, "topic_id" : 1})
{ "_id" : "1010", "date_posted" : ISODate("2010-07-05T01:27:53Z"), "topic_id" : "1004" }
{ "_id" : "106", "date_posted" : ISODate("2010-05-21T01:52:59Z"), "topic_id" : "105" }
{ "_id" : "107", "date_posted" : ISODate("2010-05-21T01:53:20Z"), "topic_id" : "105" }
{ "_id" : "1001", "date_posted" : ISODate("2010-07-04T04:09:43Z"), "topic_id" : "1000" }
{ "_id" : "1006", "date_posted" : ISODate("2010-07-04T23:21:30Z"), "topic_id" : "1004" }
{ "_id" : "1008", "date_posted" : ISODate("2010-07-04T23:44:56Z"), "topic_id" : "1007" }
{ "_id" : "1054", "date_posted" : ISODate("2010-07-08T17:53:57Z"), "topic_id" : "1053" }
Ok, it looks like my logic for getting the max date is screwed up. I had posted a different question for a related issue and the code posted by William Z in response to that question works better:
function (key, values) {
var re_date = ISODate('1970-01-01T00:00:00Z');
values.forEach(function (val) {
if ( re_date < val.date_posted )
re_date = val.date_posted;
}
);
return {date_posted : re_date}; }