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.
Error:
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.
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);
Error:
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.
{resource:“xxx”,“channel.ItemId”:“yyy”}
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 https://jsonlint.com/
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).
I strongly recommend that you take M001 course from https://university.mongodb.com/. 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" } )