我正在使用 MongoDB 3.2.1/python 3.4/pymongo/pandas 0.17(尽管后两者可能与这个问题完全无关)。
我在使用 MongoDB 查找时遇到了一个非常奇怪(而且错误)的行为。
我有一个集合,包含这样一个文档:
{
"_id" : NumberLong(-1819413477243867792),
"targetentity" : "NODOGENERICO .ag.HP_BAR_DEG_APP_1",
"tx" : false,
"ocname" : ".oc.serv6",
"specificproblem" : null,
"saf" : false,
"iscriticalnode" : null,
"checkmask" : null,
"notificationidentifier" : 1347592,
"province" : null,
"usertext" : null,
"additionaltext" : "AAA Invalid Response",
"director" : ".temip.madrids01_director",
"problemoccurences" : 1,
"usertags" : null,
"managedobject" : "NODOGENERICO .ag.HP_BAR_DEG_APP_1",
"isacceptednode" : null,
"elementcode" : null,
"state" : "Terminated",
"probablecause" : "Unknown",
"ran" : false,
"counttotal" : 1,
"locationcode" : "NULL",
"problemstatus" : "Closed",
"structurednotes" : null,
"collection" : "serv6",
"operatornotes" : null,
"alarmtype" : "CommunicationsAlarm",
"workinfo" : null,
"perceivedseverity" : "Major",
"core" : true,
"eventtime" : NumberLong(1467342666000),
"originalseverity" : "Major",
"vendor" : "Several",
"controlelementcode" : null,
"outageflag" : false,
"incident" : null,
}
这个“_id”基本上是使用 Python 3.4 的“hash”内置方法计算的哈希值。
问题是我插入后找不到任何具有此id的元素。
我试过了(此时我直接在 mongo 终端上尝试这个,但是在 Pymongo 上它得到了相同的结果):
db.getCollection('unique_alarm').find({"_id": NumberLong(-1819413477243867792)}
和
db.getCollection('unique_alarm').find({"_id": -1819413477243867792})
对于两者我都明白了:
Fetched 0 record(s) in 1ms
我认为问题在于我如何处理 NumberLong,但对于字段事件时间(具有相同类型)我完全没有问题。
即,对于事件时间,如果我查询:
db.getCollection('unique_alarm').find({"eventtime" : NumberLong(1467342666000)})
或通过:
db.getCollection('unique_alarm').find({"eventtime" :1467342666000})
这两个查询都再次返回第一个文档,没问题。
关于正在发生的事情有什么线索吗?为什么前两个查询返回 0 个结果?
关于我的试错的更多信息:
- 不管字段是“_id”还是任何其他字段,我都无法搜索这些数字
- 我正在使用 pymongo 插入这些文档
- 如果我尝试再次插入此文档(使用 pymongo 或 mongodb 终端),我会收到重复键错误...
最佳答案
答案可能是微不足道的,但所有内容都用引号 "
围绕 number long 的值连接。
插入数据和查询需要'引用'
db.sofia.find({"_id" : NumberLong("-1819413477243867792")}).pretty()
{
"_id" : NumberLong("-1819413477243867792"),
"targetentity" : "NODOGENERICO .ag.HP_BAR_DEG_APP_1",
"tx" : false,
"ocname" : ".oc.serv6",
....
}
关于python - Mongodb 查找结果错误的 Int64 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38243680/