乐闻世界logo
搜索文章和话题

How to query on the last element of an array inmongodb

2个答案

1
2

In MongoDB, you can use the $slice operator to retrieve the last element of an array. When used with the .find() or .findOne() methods, $slice allows you to specify the number and position of elements to select from the array.

If you only want to query the last element of the array, set $slice to -1. Here is an example query that demonstrates how to retrieve the last element of the arrayField array field:

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

In this example:

  • db.collection represents the collection in MongoDB.
  • {} is the query condition (empty, meaning select all documents).
  • { arrayField: { $slice: -1 } } is the projection that specifies returning only the last element of arrayField.

If you have a specific query condition and only want to retrieve the last element of the array for documents that match this condition, you can combine it as follows:

javascript
db.collection.find({ someField: someValue }, { arrayField: { $slice: -1 } })

In this example:

  • { someField: someValue } is the query condition used to select documents with a specific someField value of someValue.
  • { arrayField: { $slice: -1 } } is the projection used to select only the last element of arrayField.

Note that $slice can be used not only to retrieve the last element but also to get subsets of the array, such as the last three elements ({ $slice: -3 }) or skipping the first two elements to take the next two ({ $slice: [2, 2] }). Furthermore, let's explore additional applications of $slice to discuss how to retrieve specific ranges of elements within an array, which is useful for pagination or when only interested in certain parts of the data.

For example, if we want to retrieve three elements starting from the second element in the array, we can set $slice as follows:

javascript
db.collection.find({}, { arrayField: { $slice: [1, 3] } })

Here:

  • [1, 3] specifies starting from index 1 (the second element of the array) and retrieving the next three elements.

Additionally, MongoDB allows you to combine the $elemMatch query operator with $slice to select subsets of array elements that match specific conditions. For example, if you want to find documents containing a specific element and return only that element, you can do the following:

javascript
db.collection.find({ arrayField: { $elemMatch: { key: value } } }, { 'arrayField.$': 1 })

In this example:

  • { arrayField: { $elemMatch: { key: value } } } is the query condition that matches documents where at least one element in the arrayField array has the key key and value value.
  • {'arrayField.$': 1} is the projection that specifies returning only the first element matching the $elemMatch condition.

Advanced usage combining $slice and $elemMatch can be more complex, but the above examples demonstrate common approaches to query specific parts of an array, including the last element. In practical applications, these techniques can help you efficiently query and process data stored in MongoDB arrays.

2024年6月29日 12:07 回复

Starting from MongoDB 4.4, the aggregation operator $last can be used to access the last element of an array.


For example, in a find query:

shell
// { "myArray": ["A", "B", "C"] } // { "myArray": ["D"] } db.collection.find({ $expr: { $eq: [{ $last: "$myArray" }, "C"] } }) // { "myArray": ["A", "B", "C"] }

Or in an aggregation query:

shell
db.collection.aggregate([ { $addFields: { last: { $last: "$myArray" } } }, { $match: { last: "C" } } ])
2024年6月29日 12:07 回复

你的答案