MongoDB - Query on the last element of an array?

后端 未结 9 1117
醉梦人生
醉梦人生 2020-11-29 06:51

I know that MongoDB supports the syntax find{array.0.field:\"value\"}, but I specifically want to do this for the last element in the array, which means I don\'

9条回答
  •  孤城傲影
    2020-11-29 07:33

    use $slice.

    db.collection.find( {}, { array_field: { $slice: -1 } } )
    

    Editing: You can make use of { : { $elemMatch: { , , ... } } } to find a match.

    But it won't give exactly what you are looking for. I don't think that is possible in mongoDB yet.

提交回复
热议问题