How to find into mongodb to the last item of an array?

后端 未结 5 1657
无人及你
无人及你 2020-12-29 16:31

I want to find documents where last elements in an array equals to some value. Array elements may be accessed by specific array position:

// i.e. comments[0]         


        
5条回答
  •  没有蜡笔的小新
    2020-12-29 16:49

    I'm just doing :

    db.products.find({'statusHistory.status':'AVAILABLE'},{'statusHistory': {$slice: -1}})
    

    This gets me products for which the last statusHistory item in the array, contains the property status='AVAILABLE' .

提交回复
热议问题