db.data.insert({userAjax: 'sean',date:'26/04/2014',latv:'78.134123', lonv:'43.
342123', distance:'1.23', hours:'0', minutes:'4', seconds:'21', speed: '1.04' })
db.data.insert({userAjax: 'sean',date:'26/04/2014',latv:'78.134123', lonv:'43.
342123', distance:'1.23', hours:'0', minutes:'4', seconds:'21', speed: '1.04' })
db.data.insert({userAjax: 'sean',date:'25/04/2014',latv:'78.134123', lonv:'43.
342123', distance:'0.43', hours:'0', minutes:'2', seconds:'47', speed: '1.33' })
db.data.insert({userAjax: 'sean',date:'25/04/2014',latv:'78.134123', lonv:'43.
342123', distance:'0.43', hours:'0', minutes:'7', seconds:'55', speed: '1.33' })
这是我收藏的示例结构。在网站上我需要显示日期,但只显示该日期的最新插页,
数据来自手机(地理位置)通过ajax,在客户端,必须显示插入的最新日期。
答案 0 :(得分:0)
很可能你是按照给定用户的日期排序所以:
db.data.find({ "userAjax": "sean" }).sort({ "date": -1 }).limit(1)
在该用户的最新日期为您提供一个结果。
另一方面,如果您正在寻找每天的最后一个结果,那么您可以借助.aggregate()
函数执行此操作:
db.data.aggregate([
{ "$match": { "userAjax": "sean" } },
{ "$project": {
"_id": {
"date": {
"year": { "$year": "$date" },
"month": { "$month": "$date" },
"day": { "$dayOfMonth": "$date" }
}
},
"userAjax": "$userAjax",
"date": "$date",
"latv": "$latv",
"lonv": "$longv",
"distance": "$distance",
"hours": "$hours",
"minutes": "$minutes",
"seconds": "$seconds",
"speed": "$speed"
}},
{ "$sort": { "_id": -1 } },
{ "$group": {
"_id": { "$first": "$_id" },
"userAjax": { "$first": "$userAjax" },
"date": { "$first": "$date" },
"latv": { "$first": "$latv" },
"lonv": { "$first": "$longv" },
"distance": { "$first": "$distance" },
"hours": { "$first": "$hours" },
"minutes": { "$first": "$minutes" },
"seconds": { "$first": "$seconds" },
"speed": { "$first": "$speed" }
}}
])
这更简洁,但这就是你想要的。