2011-11-05 64 views
1

我有一个MongoDB的集合与下列文件中它的命令搜索所有具有place接近[0,0]且还有remaining > 0以及匹配的地点的coords的文档。过滤的MongoDB geoNear与查询

然而,运行命令

db.runCommand(
    { 
     'geoNear':"mycollection", 
     near:[0,0], 
     query:{'places.remaining': {'$gt': 0}}, 
     includeLocs: true 
    } 
) 

返回:

{ 
    "ns" : "test.mycollection", 
    "near" : "1100000000000000000000000000000000000000000000000000", 
    "results" : [ 
     { 
      "dis" : 0, 
      "loc" : { 
       "0" : 0, 
       "1" : 0 
      }, 
      "obj" : { 
       "_id" : ObjectId("4eb479a61258ec97acd1034d"), 
       "places" : [ 
        { 
         "coords" : [ 
          1, 
          0 
         ], 
         "remaining" : 1 
        }, 
        { 
         "coords" : [ 
          0, 
          0 
         ], 
         "remaining" : 0 
        } 
       ] 
      } 
     }, 
     { 
      "dis" : 1, 
      "loc" : { 
       "0" : 1, 
       "1" : 0 
      }, 
      "obj" : { 
       "_id" : ObjectId("4eb479a61258ec97acd1034d"), 
       "places" : [ 
        { 
         "coords" : [ 
          1, 
          0 
         ], 
         "remaining" : 1 
        }, 
        { 
         "coords" : [ 
          0, 
          0 
         ], 
         "remaining" : 0 
        } 
       ] 
      } 
     } 
    ], 
    "stats" : { 
     "time" : 0, 
     "btreelocs" : 0, 
     "nscanned" : 2, 
     "objectsLoaded" : 1, 
     "avgDistance" : 0.5, 
     "maxDistance" : 1.0000124312194423 
    }, 
    "ok" : 1 
} 

即这两个地方,即使placecoords[0,0]remaining = 0

我怀疑的是,该查询是没有任何影响,但是当我运行

db.runCommand(
    { 
     'geoNear':"mycollection", 
     near:[0,0], 
     includeLocs: true, 
     query:{'foo':'bar'} 
    } 
) 

没有地方返回:

{ 
    "ns" : "test.mycollection", 
    "near" : "1100000000000000000000000000000000000000000000000000", 
    "results" : [ ], 
    "stats" : { 
     "time" : 0, 
     "btreelocs" : 0, 
     "nscanned" : 2, 
     "objectsLoaded" : 1, 
     "avgDistance" : NaN, 
     "maxDistance" : 0 
    }, 
    "ok" : 1 
} 

没有人有任何想法是怎么回事?我想甚至可以用geoNear?提前致谢。

回答

1

这是因为您在另一个文档内有嵌入位置。即使位置操作符$存在,Mongodb也没有能力(现在)只返回子文档。 $操作符当前与更新特定的子文档一起工作。

这是过滤多级嵌入式文档的行为,通常匹配过滤器将返回整个文档,而不是子集。

所以你需要改变你的文档结构。替代地在文档中存储多个地方,将它们分割成单独的文档。它会给你想要的能力。

欲了解更多信息,请参阅How to make a query in this nested document structure (MongoDB)?