我在我的数据库中有以下记录[简化到问题的重要内容],我想知道是否有一种方法可以选择Type = 1,customerID =“Customer 1”的所有记录并按transDateTime ASC排序?谢谢。
{
type: "1",
notes: "bla bla",
transactions : [
{
"customerID" : "Customer 1",
"transDateTime" : ISODate("2017-09-10T18:54:41.983Z")
},
{
"transDateTime" : ISODate("2017-08-10T18:54:41.983Z")
},
{
"customerID" : "Customer 1",
"transDateTime" : ISODate("2017-02-10T11:54:41.983Z")
}
],
}
您可以通过点表示法访问子字段
find({ "type": "1", "transactions.customerID": "Customer 1" }).sort({ "transactions.transDateTime": 1 })
https://docs.mongodb.com/v3.2/core/document/#document-dot-notation