I have multiple duplicated entries, I think this is the problem with NoSQL type of database, although easy to use it has no normalization method like the relational type database.
So my mongo database have duplicated like this:

I have searched the web for examples of delete_many method from the internet, the contents are almost the same, examples on delete_many are terrible. All websites I have visited have the same examples i.e delete by date “$gt” certain date…
Until I found a tutorial on Mongodb which inspires me on how to do it on python.
So this is the delete query by ids:
del_query = {
"_id": {
"$in": [ObjectId('5f5f81a15998e89f89e0ea77'),
ObjectId('5f5f81a15998e89f89e0ea78'),
ObjectId('5f5f81a359c82df6500a3559')]
}
}
The entire code is like this:
from pymongo import MongoClient
from bson.objectid import ObjectId
mclient = MongoClient(mongodb_url)
mdb = mclient["first_db"]
mtable = mdb["device"]
del_query = {
"_id": {
"$in": [ObjectId('5f5f81a15998e89f89e0ea77'),
ObjectId('5f5f81a15998e89f89e0ea78'),
ObjectId('5f5f81a359c82df6500a3559')]
}
}
mtable.delete_many(del_query)
for x in mtable.find():
print(x)
The result becomes like this as expected.
