mongo搜索没有关键字

时间:2011-12-02 13:41:56

标签: mongodb

{
   "_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']}             
}

结果:没有!

我做错了什么?

1 个答案:

答案 0 :(得分:3)

答案很简单:因为关键字不是数组。要搜索关键字,您需要更改文档结构,如下所示:

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

通常在您从驱动程序序列化字典时发生。在目前的时刻,没有办法搜索这样的结构。简单使用数组而不是字典。或者您可以在序列化文档之前将字典转换为数组,反之亦然,在反序列化文档时。