2016-11-22 67 views
1

mongodb文件中的整數值保存32int。 我想在mongodb中保存64位值。使用PyMongo創建NumberLong整數

代碼是在這裏:

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) 

結果:

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

通緝的結果是:

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

其存儲在NumberInt而非NumberLong

回答

1

您需要使用bson.Int64型明確創建NumberLong變量。

import bson 

data['num'] = bson.Int64(100) 
0

基於user3100115's答案,我讀PyMongo BSONint64docs

用於創建NumberLong的正確用法是bson。 int64 .Int64

import bson 

number_long = bson.int64.Int64(100)