我在Collectionname
中的Dbname中有很多记录 "_id" : ObjectId("53e32f83bca58515b6eee86e"),
"data" : [{
"id" : "7676722",
"created_time" : "2014-03-16T17:06:49+0000"
}]
….
如何在Collectionname中的Dbname中选择created_time
的最大值?
sql analog = select max(created_time) from Dbname.Collectionname
答案 0 :(得分:1)
您可以使用aggregate
:
db.test.aggregate([
// Unwind the data array to one element per doc
{$unwind: '$data'},
// Order those by created_time descending
{$sort: {'data.created_time': -1}},
// Take the first one
{$limit: 1},
// Project just the needed value
{$project: {_id: 0, max_created_time: '$data.created_time'}}
])