Delete multiple records using mongoimport

Hi Experts,

What is the process of deleting multiple records from a collection using a json file. mongoimport utility or any any other utility to full fill this requirement. I am trying the following but getting an error.

mongoimport --host hostname:port --db=test --collection=collectionName --mode=delete --file=“C:\MongoDB\Docs\Delete.json” --authenticationDatabase $external --ssl --sslCAFile cacert.pem -u xxx --authenticationMechanism PLAIN -p

2021-05-10T17:35:43.404+0200 Failed: error processing document #1: invalid character ‘d’ looking for beginning of value
2021-05-10T17:35:43.404+0200 0 document(s) deleted successfully. 0 document(s) failed to delete.

Json File Contains:

db.collectionName.remove({resource:“xxx”,“channel.ItemId”:“yyy”}, true);

It seems your Delete.json is not actual JSON file but a command hence parser picks up invalid character ‘d’ as it is first character in the file?


I changed the file name to dl.json and getting the same error. its picking first character in the json file i.e. db.collectionName.remove({resource:“xxx”,“channel.ItemId”:“yyy”}, true);

If i modify the data in json file with the dbname ‘test’ over db like below, i am getting different error.
test.collectionName.remove({resource:“xxx”,“channel.ItemId”:“yyy”}, true);

Failed: error processing document #1: invalid character ‘e’ in literal true (expecting ‘r’)

If i modify the data in json file just as an import files with only data with out any commands as we are using --mode=delete as like below, i am getting different error.

Error: Failed: invalid JSON input

Hi Jagadeesh,

There are two different issues with your attempt but they both boil up to an invalid JSON problem.

The argument of remove() and in fact any mongo operation needs to be a JSON data. JSON data is written as name/value pairs. A name/value pair consists of a field name (in double quotes), followed by a colon, followed by a value. Appling this rule the true part in the argument to remove is missing it’s argument’s name and curly braces. Granted that mongo shell makes it a bit easier to read commands because it allows dropping double quotes for names but sematic of data needs to be followed.

The JSON data requires names to be double quoted therefore {"resource":"xxx", "channel.ItemId":"yyy"} is a valid JSON but {resource:"xxx", "channel.ItemId":"yyy"} is not quite . Validity of JSON can be tested with online validator like for example
Since the argument is an external file the content of which must be valid JSON parser complains (so no dropping quotes for name on this occasion, I’m afraid).

Hope this helps.

1 Like

Hi MaxOfLondon,

Thank you for your time to assist me and what is the better way to full fill my requirement ?

I need to deleted few 100 records from a collection on a remote mogoserver through mongo shell connecting from my laptop.

I strongly recommend that you take M001 course from That sort of things are well covered. For you requirement you might try:

C:\Users\Jagadeesh> mongo Address-Of-Your-Server
// The mongo shell is started
// Select the database you what to use
> use test 
// Simply delete the documents with
> db.collectionName.deleteMany( { "resource":"xxx" , "channel.ItemId":"yyy" } )