2011-12-02 51 views
0
{ 
    "_id": ObjectId("4ed8d496c605da94400001e4"), 
    "status": 1, 
    "user": { 
    "uid": 1 
    }, 
    "nid": 10582, 
    "form": { 
    "your-name": "Bob Smith", 
    "description": "", 
    "photo": "", 
    "address": "123 Turk Hill Rd", 
    "city": "", 
    "zip": "14450" 
    }, 
    "location": { 
    "address": "123 Turk Hill Rd", 
    "city": "", 
    "zip": "14450", 
    "geo_lat": 43.0329181, 
    "geo_lng": -77.4391148, 
    "address_confirmed": "123 Turk Hill Rd, Victor, NY 14564, USA", 
    "address_status": 200, 
    "accuracy": 8 
    }, 
    "keywords": { 
    "0": "bob", 
    "1": "smith", 
    "2": "", 
    "4": "123", 
    "5": "turk", 
    "6": "hill", 
    "7": "rd", 
    "9": "14450" 
    }, 
    "time": ISODate("2011-12-02T13: 37: 26.0Z") 

搜索:蒙戈搜索不到工作關鍵字

{ 
    nid: 10582, 
    keywords: {"$in": ['turk']}    
} 

結果:沒有!

我在做什麼錯?

回答

3

答案很簡單:因爲關鍵字不是數組。要搜索關鍵字,您需要更改文檔結構如下:

{ 
... 
"keywords": [ 
    "bob", 
    "smith", 
    "123", 
    "turk", 
    "hill", 
    "rd", 
    "14450" 
    ], 
... 
} 

它通常發生在您從驅動程序序列化字典中。在目前的時刻,沒有辦法在這樣的結構中進行搜索。簡單地使用數組而不是字典。或者,您可以在序列化文檔之前將字典轉換爲數組,反序列化文檔時也可以將字典轉換爲數組。

+0

良好的通話,我錯過了 –