我拥有的MongoDB集合:
/* 1 */
{
"_id" : ObjectId("5f4c93478ac8f4f9d79151bd"),
"property" : "prop_1",
"created" : ISODate("2020-01-01T22:00:00.000Z")
}
/* 2 */
{
"_id" : ObjectId("5f4c93628ac8f4f9d79151be"),
"property" : "prop_1",
"created" : ISODate("2020-01-02T22:00:00.000Z")
}
/* 3 */
{
"_id" : ObjectId("5f4c93708ac8f4f9d79151c0"),
"property" : "prop_2",
"created" : ISODate("2020-01-01T22:00:00.000Z")
}
/* 4 */
{
"_id" : ObjectId("5f4c93738ac8f4f9d79151c1"),
"property" : "prop_2",
"created" : ISODate("2020-01-02T22:00:00.000Z")
}
我在查询属性property
等于某个特定值的最旧文档时遇到了麻烦,例如,我需要查找属性property
等于prop_1
的最旧文档:
/* 1 */
{
"_id" : ObjectId("5f4c93478ac8f4f9d79151bd"),
"property" : "prop_1",
"created" : ISODate("2020-01-01T22:00:00.000Z")
}
你能提供一些建议吗?
2条答案
按热度按时间wdebmtf21#
使用$match按
property
、$sort和$limit进行过滤,以获得最旧的一个:Mongo Playground
gev0vcfq2#
您可以将find()与
sort
和limit
方法一起使用,