Here is my schema;
...
edge: {
name: [
{
type: String
}
],
image: [
{
type: String
}
]
},
user: {
type: mongoose.Types.ObjectId,
ref: 'users'
},
...
I want to select last edge.name
and edge.image
field. For example;
MySchema.find({user: req.user._id},{_id:0, edge:1})
But edge come 4 name. I want to just last one, how can I do that?
CodePudding user response:
You can use $slice with a negative number n
as projection to get the last n
elements of array. In your case it should be:
MySchema.find(
{user: req.user._id},
{
"edge.name": {
"$slice": -1
},
"edge.image": {
"$slice": -1
}
}
)