Anda perlu $unwind
, $group
dan gunakan $first
dan $last
operator seperti ini:
db.collection.aggregate([
{ "$unwind": "$favorites" },
{ "$group": {
"_id": "$_id",
"first": { "$first": "$favorites" },
"last": { "$last": "$favorites" }
}}
])
Atau dua kueri berbeda jika $unwind
mahal
var first = db.collection.find({}, {"favorites": { $slice: 1}})
var last = db.collection.find({}, {"favorites": { $slice: -1}})