Gandi Pirkov 愛
Gandi Pirkov 愛

Reputation: 90

Importing json file in new fresh mnogodb

I just want to ask how can I import this example.json file in new mongodb I expect to have each seassion object as row in the table I tried

mongoimport --db foo --collection myCollections < dataBuys.json
2015-05-07T21:19:15.828+0300    connected to: localhost
2015-05-07T21:19:18.831+0300    foo.myCollections   168.5 MB
2015-05-07T21:19:21.826+0300    foo.myCollections   168.5 MB
2015-05-07T21:19:24.828+0300    foo.myCollections   168.5 MB
2015-05-07T21:19:27.828+0300    foo.myCollections   168.5 MB
2015-05-07T21:19:28.849+0300    warning: attempting to insert document      with size 124.6 MB (exceeds 16.0 MB limit)
2015-05-07T21:19:28.986+0300    error inserting documents: write tcp 127.0.0.1:27017: broken pipe
2015-05-07T21:19:28.986+0300    imported 0 documents

and this

mongoimport -d mydb -c mycollection --jsonArray < dataBuys.json
2015-05-07T21:20:02.139+0300    connected to: localhost
2015-05-07T21:20:02.139+0300    Failed: error reading separator after document #1: bad JSON array format - found no opening bracket '[' in input source
2015-05-07T21:20:02.139+0300    imported 0 documents

The file I want to import have the following format and it size is 170mb for this one and 2.97GB for the other one.

{
"Sessions": {
"420374" : {
  "Purchases" : [
    {
      "Price" : "12462",
      "Quantity" : "1",
      "Timestamp" : "2014-04-06T18:44:58.314Z",
      "ItemId" : "214537888"
    },
    {
      "Price" : "10471",
      "Quantity" : "1",
      "Timestamp" : "2014-04-06T18:44:58.325Z",
      "ItemId" : "214537850"
    }
  ]
},

"281626" : {
  "Purchases" : [
    {
      "Price" : "1883",
      "Quantity" : "1",
      "Timestamp" : "2014-04-06T09:40:13.032Z",
      "ItemId" : "214535653"
    }
  ]
},

"420368" : {
  "Purchases" : [
    {
      "Price" : "6073",
      "Quantity" : "1",
      "Timestamp" : "2014-04-04T06:13:28.848Z",
      "ItemId" : "214530572"
    },
    {
      "Price" : "2617",
      "Quantity" : "1",
      "Timestamp" : "2014-04-04T06:13:28.858Z",
      "ItemId" : "214835025"
    }
  ]
}
}
}

Do I have to reformat the json ? is it possible to make it work like this ?

Upvotes: 1

Views: 3304

Answers (2)

Menelaos Kotsollaris
Menelaos Kotsollaris

Reputation: 5506

First of all, for verifying that your GeoJSON file is accurate, you could use Geojsonlint, QGIS and so on.

After than, to import your data into your collection, use Mongoimport:

mongoimport --db MY_DATABASE_NAME -c MY_COLLECTION_NAME --type json --file "MY_GEOJSON_FILENAME"

Replace the 3 variables above whith your valid names. Obviously, make sure that your current directory contains the file.

Profit! :)

Upvotes: 1

Pat Claffey
Pat Claffey

Reputation: 76

the first error message says: warning: attempting to insert document with size 124.6 MB (exceeds 16.0 MB limit)

This implies you are trying to insert a document that is 124.6MB in size. A json document starts with an open brace character "{" and ends with a closed brace character "}". The error message implies that you have 124.6MB between such characters.

I think you need to examine your input file and verify that each session object is defined as a separate document - another words starts and ends with a brace.

I suspect the problem is that the session objects are in fact embedded in a master document - sort of a container document. This would make mongoimport try to map the master container document to its collection - and not the session objects as you require.

Upvotes: 3

Related Questions