MongoDB: Should i do all crud operation using username or objectId or use username to find objectId and then use this id to do crud operation?
1> Using ObjectId is not convenient from end user perspective
await User.findByIdAndDelete({req.params.id});
2> Using username is more convenient from end user perspective
await User.findByIdAndDelete({req.params.id})
3>Provide API endpoints or methods that allow querying and updating users using usernames. Internally, resolve these usernames to ObjectIDs for database operations.
async function getChatByUsername(username) {
const user = await User.findOne({ username: username });
if (user) {
return await Chat.find({ userId: user._id });
}
throw new Error('User not found');
}
logically all are correct but which one is optimised?
just want to know which one is more efficient
Vishal Chauhan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.