My collection has data with 10000+ records in the fields. It has two documents with 111 fields and 7 fields respectively.
I want to delete the document in collection with 7 fields. Please help suggest a query for this problem.
>Solution :
To delete a document with 7 fields, use the db.collection.deleteOne() method with the $expr query operator which allows you to use aggregate operators like $size and $objectToArray to get the number of fields in the document using the system variable $$ROOT as reference to the root document.
The following operation will delete a single document with n number of fields:
var n = 7;
db.collection.deleteOne({
$expr: {
$eq: [
{ $size: { $objectToArray: '$$ROOT' } },
n // n = 7
]
}
});
