2

In Mongodb v2.2 When I try to import one simple json document file like this from my .json file into an empty collection I get 13 objects imported. Here is what I'm doing.

this is the data (I've shortened the field names to protect data):

[
{ 
    "date" : ISODate("2012-08-01T00:00:00Z"), 
    "start" : ISODate("2012-08-01T00:00:00Z"), 
    "xxx" : 1, 
    "yyt" : 5, 
    "p" : 6, 
    "aam" : 20, 
    "dame" : "denon", 
    "33" : 10, 
    "xxt" : 8, 
    "col" : 3, 
    "rr" : [   
        { "name" : "Plugin 1", "count" : 1 },  
        { "name" : "Plugin 2", "count" : 1 },  
        { "name" : "Plugin 3", "count" : 1 }   
    ], 
    "xkx" : { "y" : 0, "n" : 1 }, 
    "r" : { "y" : 0, "n" : 1 }, 
    "po" : { "y" : 0, "n" : 1 }, 
    "pge" : { "posts" : 0, "pages" : 1 }, 
    "pol" : { "y" : 0, "n" : 1 }, 
    "lic" : { "y" : 0, "n" : 1 }, 
    "count" : 30, 
    "tx" : [ 
        { "zone" : -7, "count" : 1 } 
    ], 
    "yp" : "daily", 
    "ons" : [ 
        { "version" : "9.6.8", "count" : 1 } 
    ], 
    "ions" : [
        { "version" : "10.0.3", "count" : 1 } 
    ] 
}
]

with this command:

mongoimport --db development_report --collection xxx --username xxx --password xxx --file /Users/Alex/Desktop/daily2.json --type json --jsonArray --stopOnError --journal

I get this weired response:

Mon Sep  3 12:09:12 imported 13 objects

and this 13 new documents end up in the collection instead of one:

{ "_id" : ObjectId("5044114815e24c08bcdc988e") }
{ "_id" : ObjectId("5044114815e24c08bcdc988f"), "name" : "Plugin 1", "count" : 1 }
{ "_id" : ObjectId("5044114815e24c08bcdc9890"), "name" : "Plugin 2", "count" : 1 }
{ "_id" : ObjectId("5044114815e24c08bcdc9891"), "name" : "Plugin 3", "count" : 1 }
{ "_id" : ObjectId("5044114815e24c08bcdc9892"), "y" : 0, "n" : 1 }
{ "_id" : ObjectId("5044114815e24c08bcdc9893"), "y" : 0, "n" : 1 }
{ "_id" : ObjectId("5044114815e24c08bcdc9894"), "y" : 0, "n" : 1 }
{ "_id" : ObjectId("5044114815e24c08bcdc9895"), "posts" : 0, "pages" : 1 }
{ "_id" : ObjectId("5044114815e24c08bcdc9896"), "y" : 0, "n" : 1 }
{ "_id" : ObjectId("5044114815e24c08bcdc9897"), "y" : 0, "n" : 1 }
{ "_id" : ObjectId("5044114815e24c08bcdc9898"), "zone" : -7, "count" : 1 }
{ "_id" : ObjectId("5044114815e24c08bcdc9899"), "version" : "9.6.8", "count" : 1 }
{ "_id" : ObjectId("5044114815e24c08bcdc989a"), "version" : "10.0.3", "count" : 1 }

What am I doing wrong?

Roman
  • 10,309
  • 17
  • 66
  • 101

3 Answers3

5

The problem you are having is with the two ISODate fields you have at the start of your document.

JSON does not have any "date" type, so it does not handle ISODate fields in your document. You would need to convert these like so:

[
{ 
"date" : { "$date" : 1343779200000 }, 
"start" : { "$date" : 1343779200000 },
...

And your import will work.

The reason this comes about is because MongoDB handles more types than are available in the JSON spec. You can see more information in the documentation. There is also an open ticket to make MongoImport handle all the formats MongoDB does here and more details here

Andre de Frere
  • 2,703
  • 16
  • 13
1

This is really frustrating; I couldn't get anywhere fast with the import tool so I used the load() function within the mongo client to load a script which inserted my records.

> load('/Users/Alex/Desktop/daily.json');

I obviously had to modify the json file to include insert commands like so:

>db.mycollection.insert(
    { DOCUMENT 1 },
    ...
    { DOCUMENT N }
);
Roman
  • 10,309
  • 17
  • 66
  • 101
  • Great workaround for `mongoimport` not supporting `ISODate`. However, you have to pass `insert` either a single object or an array - then it worked perfectly for me. – tobek Dec 01 '14 at 09:14
0

This is really late, but in case it can help anyone else - you should not be passing a JSON array. Simply list 1 JSON document per line, and each line will create a separate document. The file below would insert 2 documents:

{ "date" : { "$date": 1354320000000 }, "xxx" : 1, "yyt" : 5, ... }
{ "date" : { "$date": 1354320000000 }, "xxx" : 2, "yyt" : 6, ... }
Jerry Clinesmith
  • 424
  • 5
  • 10