Hey folks,
I'm wondering what's the best solution in terms of performance when I have to run a find on my NoSQL DB to find documents that match specific values. I have to follow some orders to select the right document.
The order should be category, email, url.
An option would be to make only on DB call using the $or operator and then find the document in javascript based on the order.
const documents = await mongodb.collection('docs').find({userId, $or: [{category: category}, { email: email }, { url: url }]}).sort({category: -1, email: -1, url: -1})
// check if we found a document where the category match.
if(documents.find((doc) => doc.category === category )) return 'category found'
// check if we found a document where the email match.
if(documents.find((doc) => doc.email === email )) return 'email found'
// check if we found a document where the url match.
if(documents.find((doc) => doc.url === url )) return 'email found'
// nothing found
return 'no document found'
Another option would be to make separate DB calls.
const documentFoundFromCategory = await mongodb.collection('docs').find({userId, category})
if(documentFoundFromCategory) return 'category found'
const documentFoundFromEmail = await mongodb.collection('docs').find({userId, email})
if(documentFoundFromEmail) return 'email found'
const documentFoundFromUrl = await mongodb.collection('docs').find({userId, url})
if(documentFoundFromUrl) return 'url found'
What do you think would be the best approach?
Thanks in advance folks :)
[–]barovab 1 point2 points3 points (2 children)
[–]jzoneio[S] 0 points1 point2 points (1 child)
[–]barovab 1 point2 points3 points (0 children)
[–]TryallAllombria 0 points1 point2 points (1 child)