使用 PyMongo 创建 NumberLong 整数

Imr*_*ran 8 python mongodb pymongo mongodb-query

mongodb 中的整数值记录了其保存的 32int。我想在 mongodb 中保存 64 位值。

代码在这里:

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)
Run Code Online (Sandbox Code Playgroud)

结果:

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

想要的结果是:

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

它存储在 NumberInt 而不是 NumberLong

sty*_*ane 12

您需要NumberLong使用该bson.Int64类型显式创建变量。

import bson

data['num'] = bson.Int64(100)
Run Code Online (Sandbox Code Playgroud)


Dan*_*orn 6

根据user3100115 的回答,我阅读了 PyMongo BSON int64 docs

创建a的正确用法NumberLong是bson。int64 .Int64

import bson

number_long = bson.int64.Int64(100)
Run Code Online (Sandbox Code Playgroud)