Ran a query while there was no field named "createdOn" present in the collection and so that entire collection documents were deleted

Actually, we were using a query to purge one of the collection data but recently one of the fields (createdOn) was removed and when the query has run again the entire collection data was deleted.
so just want to know how MongoDB works if any field is removed and if any query uses that field, will there be an error thrown?

Please find the query below:
Query: { “createdOn” : { “$lt” : { “$date” : “2022-04-14T04:59:47.076Z”}}}, Fields: {}, Sort: {}

No MongoDB will not throw an error if you do a find on a field that doesn’t exist see below an example in python and the shell

Mongo shell (returns with no errors)

> db.servers.find({"asdf": "a;lkdjf"})

Python (pymongo)

>>> for a in coll.find({'field_doesnt_exist': 'random value'}):
...     print("{}".format(a))

Can you share the code including the delete command?

I suspect something else deleted the collection. May deleteMany() has been used wrongly.

mongosh> c.find()
// all documents
{ _id: 0 }
{ _id: 1, date: null }
{ _id: 2, date: 2022-05-07T22:14:16.825Z }

mongosh> c.find().sort(  { date : 1 } )

// all documents sorted by date, null and missing date are included and are comes before
{ _id: 0 }
{ _id: 1, date: null }
{ _id: 2, date: 2022-05-07T22:14:16.825Z }

mongosh> c.find( { date : { $lt : new Date( "2022-05-08")}})
// when we find by date we only find documents with an existing date field
{ _id: 2, date: 2022-05-07T22:14:16.825Z }

mongosh> c.find( { date : { $gt : new Date( "2022-05-08")}})
// this is confirmed by finding no document

mongosh> c.deleteMany(  { date : { $lt : new Date( "2022-05-08")}})
// in theory only a document that can be found by a query will be delete by the same query
// and that is confirmed by
{ acknowledged: true, deletedCount: 1 }
// and by looking at the remaining document
mongosh> c.find()
{ _id: 0 }
{ _id: 1, date: null }

So something else deleted your documents.

However, I have no difficulty seeing someone making an error while issuing a command like:

mongosh> c.deleleMany({},{“createdOn” : { “$lt” : { “$date” : “2022-04-14T04:59:47.076Z”}}}

// all documents will be delete.

sorry for the delayed reply. Please find the delete command.


Thanks for your reply. Below was the delete query used.


Your original post was mentioning a query with the field createdOn:

and now the query is

which has not mentioned of createdOn at all. But may be is referred in the part that you have truncated before posting.

I also wonder why your original post mentioned Fields and Sort in

for a query used for deleting.

So just to summarize the thread.

  1. MongoDB will not throw an error when a query specifies a field that does not exist, like createdOn in your query.
  2. MongoDB will not match a document that does not have the field when using comparative operators like $lt in your query. Not for find(), not for delete() and not for update().
  3. Documents that have been deleted and that did not have the field createdOn have been deleted by another query. Not by the query you shared in your first post.
1 Like

Can you post the complete code fragment. The query and the delete?