I'd like to fetch a single element within an array in my document, without fetching the whole array itself. Is that possible to do using Mongoose?
I've tried using dot.notation but it doesn't seem to work - It just fetches empty nested objects after the "index dot"
// Here I'd like to get
// The first undo-ed item from the first board within my `boardBucket`
Room.findOne({name: this.name}, `boardBucket.items.0.undoedItems.1`, (err, result)=> {
if (err) throw err;
console.log(JSON.stringify(result, null, 4));
})
Is there any way to achieve this?
Reasons I'd like to avoid fetching the whole array
- My objects are quite large and would result in memory issues if I fetched them back in their entirety
- It's inefficient however one looks at it.
Here's my Schema in case it helps:
var roomSchema = new mongoose.Schema({
name: String,
boardBucket: {
currentBoardId: String,
items: [
{
boardId: String,
boardItems: Array,
undoedItems: Array, // <- Need to get a *single* element from this array
viewPosition: String
}
]
}
});