2016-07-07 43 views
2

我使用的是MongoDB 3.2.1/python 3.4/pymongo/pandas 0.17(雖然後兩者可能完全不相關)。Mongodb查找結果不正確Int64對象

我在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的內置方法的哈希計算。

問題是,我插入它後找不到具有此ID的任何元素。

我試過(在這一點上,我蒙戈終端上直接嘗試這一點,但在Pymongo能把我相同的結果):

db.getCollection('unique_alarm').find({"_id": NumberLong(-1819413477243867792)}

db.getCollection('unique_alarm').find({"_id": -1819413477243867792})

而對於我得到這個:

Fetched 0 record(s) in 1ms

我認爲問題是關於我如何處理NumberLong,但對於現場活動時間(它有相同的類型)我完全沒有問題。

即,與EVENTTIME如果我查詢:

db.getCollection('unique_alarm').find({"eventtime" : NumberLong(1467342666000)})

或:

db.getCollection('unique_alarm').find({"eventtime" :1467342666000})

這兩個查詢再次返回這個頭文件,沒有問題。

關於發生了什麼的任何線索?爲什麼前兩個查詢返回0結果?在我的試驗和錯誤

的更多信息:

  • 如果字段是「_id」或任何其他領域,我不能搜索這些數字也並不重要
  • 我使用插入這些文件pymongo
  • 如果我嘗試再次插入該文件(使用pymongo或MongoDB的終端),我得到重複鍵的錯誤...
+0

如果任何人有興趣,我的解決方法是轉換類型** **字符串。所以基本上,在插入之前,我將數字轉換爲字符串,然後通過字符串值進行搜索,並且完美地工作。我的猜測是,某些類型的NumberLong和Mongodb搜索存在一些問題(可能與python hash的結果有關) –

回答

0

答案可能是微不足道的,但所有的數字都與長度上的價值相關聯的報價"

插入數據和查詢需要被「引用」

db.sofia.find({"_id" : NumberLong("-1819413477243867792")}).pretty() 

{ 
     "_id" : NumberLong("-1819413477243867792"), 
     "targetentity" : "NODOGENERICO .ag.HP_BAR_DEG_APP_1", 
     "tx" : false, 
     "ocname" : ".oc.serv6", 
     .... 
} 

quotes

+0

它的工作,謝謝!你知道爲什麼其他領域的'eventtime'返回OK,即使沒有引號?它不應該返回0結果嗎?我想這就是爲什麼我從來沒有用引號試過。 –

+0

它可能是由shell的JavaScript限制,但我不是100%確定 – profesor79

0

我想你打小號在mongo NumberLong中存在某種限制。

我已經開了一個蒙戈控制檯,這是輸出

> NumberLong(-1819413477243867792) 
NumberLong("-1819413477243867904") 

所以我會假設,如果通過NumberLong(「 - 1819413477243867904」)發現,你會神奇地發現你的記錄,這可能會證明如果NumberLong是你的散列,那麼它會觸及某種mongo db限制。