existOid method
Checks if a document with the given ObjectId exists in the collection.
This method performs the actual existence check using the MongoDB ObjectId directly, providing better performance than string-based checks.
Parameters:
id
- The MongoDB ObjectId to check
Returns true
if the document exists, false
if it doesn't exist
or if the ID is null.
Example:
var objectId = ObjectId.fromHexString('507f1f77bcf86cd799439011');
bool exists = await collection.existOid(objectId);
Implementation
Future<bool> existOid(ObjectId? id) async {
if (id == null) return false;
var count = await collection.modernCount(selector: where.id(id));
return count.count > 0;
}