EmbeddedDocument Class Properties

25 views
Skip to first unread message

Oscar Chiriboga

unread,
Oct 7, 2024, 4:49:46 PM10/7/24
to Mongoose Node.JS ODM
Hi everybody!

My model has an array of subdocuments. Each subdocument has its own _id. In a part of my code I would  like to get a subdocument by its _id, and then get its index. Right now, I'm doing something like this: 

const document = await MyModel.findById(MyModelId);
const subDocument = document.subDocuments && document.subDocuments.id(subDocumentId);
const subDocumentIndex = subDocument && subDocument.__index;

The code is working fine! However, I would like to know if its completely safe to do this. Thanks for your help!
Reply all
Reply to author
Forward
0 new messages