I have an array of objects as: { id: '1582715', email: 'email@gmail.com', isUsed: false }, and I have many documents in MongoDB with same keys.
Before writing new array of objects in BD, I want to check does objects of array already been written to DB.
I wrote this func that returns array of objects that hasn't been written yet:
const forWriting = testimonalsArray.filter(isWritten => {
if (collection.countDocuments({ id: { $eq: isWritten.id } }) < 1) return isWritten
})
console.log(forWriting)
BUT collection.countDocuments({ id: { $eq: isWritten.id } } returns Promise { <pending> }. I've tried add async await:
const forWriting = testimonalsArray.filter(async isWritten => {
if (await collection.countDocuments({ id: { $eq: isWritten.id } }) < 1) return isWritten
})
console.log(forWriting)
but still didn't receive any result. How should I resolve this promise to have an integer to compare it with 1?