Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create NumberLong integer using PyMongo

Integer value in mongodb document its saving 32int. I want to save 64bit values in mongodb.

code is here:

import time
import datetime
from pymongo import MongoClient
client = MongoClient()
client = MongoClient('localhost', 27017)
db = client.test_database
data = {}
data['num'] = 100
data['createAt'] = datetime.datetime.now()
curTime = datetime.datetime.now()
curTime =  int(time.mktime(curTime.timetuple()))
data['time'] = curTime
db.test.insert(data)

Result:

{ 
     "_id" : ObjectId("583420ce7e60a74345c97624"), 
     "num" : NumberInt(100), 
     "createAt" : ISODate("2016-11-22T15:41:18.773+0000"), 
     "time" : NumberInt(1479811278) 
}

Wanted result is:

{ 
     "_id" : ObjectId("583420ce7e60a74345c97624"), 
     "num" : NumberLong(100), 
     "createAt" : ISODate("2016-11-22T15:41:18.773+0000"), 
     "time" : NumberLong(1479811278) 
}

its stored in NumberInt rather than NumberLong

like image 274
Imran Avatar asked Nov 22 '16 11:11

Imran


2 Answers

You need to explicitly create your NumberLong variable using the bson.Int64 type.

import bson

data['num'] = bson.Int64(100)
like image 88
styvane Avatar answered Oct 17 '22 03:10

styvane


Based on user3100115's answer, I read PyMongo BSON int64 docs.

The correct usage to create a NumberLong is bson.int64.Int64

import bson

number_long = bson.int64.Int64(100)
like image 30
Daniel Korn Avatar answered Oct 17 '22 01:10

Daniel Korn