我试图找到一种查询mongodb集合的方法:在'history
'数组中,'running
'的state
值为also the latest entry的所有文档都应该被返回.
我可以在C#中使用.max()和.OrderBy()使用LINQ,but not in nodeJS.
C#不含polymer :
var filter = Filter.Where(w => w.history.OrderBy(o => o.createdAt).Last().status == "running")
以下是示例文档:
{
_id: 1,
name: 'name 1',
history: [
{
createdAt: '2024-01-01',
state: 'initialized'
},
{
createdAt: '2024-01-15',
state: 'running'
},
{
createdAt: '2024-02-02',
state: 'stopped'
}
]
},
{
_id: 2,
name: 'name 2',
history: [
{
createdAt: '2024-01-01',
state: 'initialized'
},
{
createdAt: '2024-01-20 01:15:66',
state: 'running'
},
{
createdAt: '2024-01-21 00:55:59', // this is the LATEST entry, and state=running --> return document
state: 'running'
},
{
createdAt: '2024-01-21 00:55:58',
state: 'stopped'
}
]
}
A state can be there more than once, so the history array could contain 'running' 3 times. In that case I would need to return the latest entry (using createdAt). It being the last entry in the array does not mean its createdAt date is the latest date.