mongoimport unable to import $numberLong

A collection with NumberLong type data as follows :


> db.hc_whois.findOne();
{
       "_id" : NumberLong(3000001),
       "startIpInLong" : NumberLong(1543503872),
       "endIpInLong" : NumberLong(1544552447),
       "name" : "ap-net-1",
       //...
 }   

Export it with mongoexport :

server1

$ mongoexport -d mydb -c hc_whois -o whois.json
whois.json

{ 
  "_id" : { "$numberLong" : "3000001" }, 
  "startIpInLong" : { "$numberLong" : "1543503872" }, 
  "endIpInLong" : { "$numberLong" : "1544552447" 
}

1. Problem

Copy the exported data whois.json to another server, and import it with mongoexport.

server2

$ mongoimport -d mydb -c hc_whois --file whois.json

The imported data look weird, an extra $numberLong is inserted, and the long value is unable to search anymore.


> db.hc_whois.findOne();
{
        "_id" : {
                "$numberLong" : "3000001"
        },
        "startIpInLong" : {
                "$numberLong" : "1543503872"
        },
        "endIpInLong" : {
                "$numberLong" : "1544552447"
        },
        "name" : "ap-net-1",
		//...
}

Why mongoimport didn’t convert the $numberLong into long value?

2. Solution

After a quick check, find out that server1 is using MongoDB v2.6.4 to export the data, and try to import the data into a server2, where MongoDB v2.4.3 is installed.

To fix it, make sure both server1 and server2 are using the same MongoDB version, look like the exported $numberLong value is supported in version 2.6.x only.

1. Refer to this guide to upgrade MongoDB to 2.6, it’s simple, just stop the MongoDB process and replace the $mongo\bin folder with the latest.

2. After server2 is upgraded to MongoDB 2.6, try importing it again.

server2

$ mongoimport -d mydb -c hc_whois --file whois.json

> db.hc_whois.findOne();
{
        "_id" : 3000001,
        "startIpInLong" : 1543503872,
        "endIpInLong" : 1544552447,
        "name" : "ap-net-1"
		//...
}

Done.

author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
3000
newest oldest most voted
kristi
Guest
kristi

2.6.2 is importing your numbers as integers, not longs. Which is better than importing the “$numberLong” objects, but still not expected behavior. You should use mongodump and mongorestore instead.