0

I'm trying to import a JSON file to MongoDB but getting the below errors

VK360@VK360:~$ mongoimport --db cstore --collection stores --drop --file /home/kuliza360/Desktop/Office/Cstoredemo/my_data.json
2018-04-04T10:37:41.545+0530    connected to: localhost
2018-04-04T10:37:41.545+0530    dropping: cstore.stores
2018-04-04T10:37:41.545+0530    Failed: not authorized on cstore to execute command { drop: "stores" }
2018-04-04T10:37:41.545+0530    imported 0 documents

I Followed the Link and performed the steps mentioned below in the mongo shell but still have the same error.

use admin
db.createUser(
  {
    user: "admin",
    pwd: "password",
    roles: [ { role: "root", db: "admin" } ]
  }
);
exit; 

Error after aove execution

2018-04-04T10:33:58.124+0530 E QUERY    [thread1] Error: couldn't add user: not authorized on admin to execute command { createUser: "admin", pwd: "xxx", roles: [ { role: "root", db: "admin" } ], digestPassword: false, writeConcern: { w: "majority", wtimeout: 60000.0 } } :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.createUser@src/mongo/shell/db.js:1267:15
@(shell):1:1
user7422128
  • 902
  • 4
  • 17
  • 41

2 Answers2

1

Add --jsonArray before --file

mongoimport --db cstore --collection stores --drop --jsonArray --file /home/kuliza360/Desktop/Office/Cstoredemo/my_data.json

Hopefully this can help other people as well!

dweb
  • 11
  • 2
0

Use the query,

mongoimport --db cstore --collection stores --drop --file /home/kuliza360/Desktop/Office/Cstoredemo/my_data.json --username admin --password password

instead of

mongoimport --db cstore --collection stores --drop --file /home/kuliza360/Desktop/Office/Cstoredemo/my_data.json

Rilwan
  • 88
  • 11
  • 2018-04-04T14:52:40.728+0530 Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed. 2018-04-04T14:52:40.728+0530 imported 0 documents – user7422128 Apr 04 '18 at 09:23
  • Try this mongoimport --db cstore --collection stores --drop --file /home/kuliza360/Desktop/Office/Cstoredemo/my_data.json --username admin --password password --authenticationDatabase admin – Rilwan Apr 04 '18 at 09:28
  • 2018-04-04T15:18:23.131+0530 Failed: error unmarshaling bytes on document #0: JSON decoder out of sync - data changing underfoot? – user7422128 Apr 04 '18 at 09:48
  • this is because of your json file. can you share it – Rilwan Apr 04 '18 at 10:06
  • [ReportDate "2018-03-08T00:00:00" ActualSalesPrice 1.59 CSPAccountNo "414693" DiscountCount 0 Description "TIC TAC GUM WATERMEL" ZIP "60098" PromotionAmount 0 POSCode "00980030003" RefundAmount 0 Totalsales 1.59 TotalCount 1 RefundCount 0 DiscountAmount 0 PromotionCount 0 POSCodeModifier "0 ] – user7422128 Apr 04 '18 at 10:11
  • Above is only a snippet of data – user7422128 Apr 04 '18 at 10:12
  • this doesn't looks like a valid json – Rilwan Apr 04 '18 at 10:15
  • can you send your json file – Rilwan Apr 05 '18 at 08:51