I am running a MongoDB in production. Today I woke up to one of my production collection of documents completely deleted. Luckily I have back up data and it is not a huge project, but the cause is still a mystery to me.
The code has been running fine for 6 months without a single problem. Today at 10am everything from one of my collections was deleted.
This is the entry in the log. I am replacing the db name and collection name with "myDB" and "cars".
2017-06-04T10:46:43.412+0200 I COMMAND [conn203614] command myDB.$cmd command: delete { delete: "cars", deletes: [ { q: {}, limit: 0 } ], ordered: true, writeConcern: { w: 1 } } keyUpdates:0 writeConflicts:0 numYields:0 reslen:40 locks:{ Global: { acquireCount: { r: 26393, w: 26393 } }, Database: { acquireCount: { w: 26393 } }, Collection: { acquireCount: { w: 26393 } } } protocol:op_query 52921ms
At first I actually believed the database might have been attacked because
delete { delete: "cars", deletes: [ { q: {}, limit: 0 } ] }
Looked pretty brutal and upfront to me. No where in my code am I running delete {}. But now I looked through everything, and there is a particular instance where I am deleting all "cars" in the collection that fit a certain query (belonging to a user). I think that is where the error might have happened, but I just cannot understand how. This is the code in question. I have altered the query for "user.find" in this example but I assure you that it is nothing more complex.
models.user.find( { something: "cool"}, '_id', function(err,result)
{
result.forEach(function(user) {
models.car.remove({owner:user._id},function(err){if(err) console.log(err)})
})
});
So, basically, the important line that I believe caused the issue:
models.car.remove({owner:user._id}, ... )
This has been running for months and has never failed, until today. My first suspicion was obviously that the user object was faulty, for example user.id being undefined. So i test:
models.car.remove({owner:undefined}, ... )
And it does not delete anything, nor does it throw an error. HOWEVER Obviously, when I run
models.car.remove({}, ... )
It drops the contents of the collection. It also generates the exact same entry in the log, which is why I believe that this code caused the error. Something must have affected the {owner:user._id} to turn into the equivalent of {}.
I just dont understand how. Really looking to solve this issue fast, any help is appreciated.
via jdokke123
No comments:
Post a Comment