Unable to filter MongoDB collection
What's the proper way to filter a collection in MongoDB? I need to filter the array by the ID number.
var collection = db.collection('blog');
try {
collection.find().toArray(function(err, result) {
if (err) {
console.log('Error:', err);
} else {
res.render('blogEntry', {
title: 'Blog Entry',
session: req.session,
blog: result
});
db.close();
}
});
} catch (Exception) {
console.log('there was a problem when accessing collection');
}
Solution 1:
collection.find({ _id: "apple" }).toArray...
The method takes two parameters:
collection.find(query, projection)
If you leave them empty, you fetch everything.
Here is the documentation