"query"매개 변수를 명령에 추가하면 찾고있는 결과를 얻을 수 있습니다.
쿼리가 작동하지 않는 이유 중 하나는 'i'플래그로 대/소문자를 구분하지 않는 한 정규식이 대/소문자를 구분한다는 것입니다. 예를 들어
은 :
> db.places.save({_id:1, city:"CityA", loc:[56.01,11.01]})
> db.places.save({_id:2, city:"CityB", loc:[56.02,11.02]})
> db.places.ensureIndex({loc:"2d"})
> db.runCommand({ geoNear: "places", near:[56,11], maxDistance:1, distanceMultiplier:1, query: { "city": /^city/} })
{
"ns" : "test.places",
"near" : "1100100000111111111100110000110000111111111100110000",
"results" : [ ],
"stats" : {
"time" : 0,
"btreelocs" : 0,
"nscanned" : 2,
"objectsLoaded" : 2,
"avgDistance" : NaN,
"maxDistance" : 0
},
"ok" : 1
}
아무 것도 반환되지 않습니다. 이제 쿼리의 정규식 부분에 대/소문자를 구분하지 않는 플래그가있는 명령이 다시 실행됩니다.
> db.runCommand({ geoNear: "places", near:[56,11], maxDistance:1, distanceMultiplier:1, query: { "city": /^city/i} })
{
"ns" : "test.places",
"near" : "1100100000111111111100110000110000111111111100110000",
"results" : [
{
"dis" : 0.014142135623729393,
"obj" : {
"_id" : 1,
"city" : "CityA",
"loc" : [
56.01,
11.01
]
}
},
{
"dis" : 0.02828427124746381,
"obj" : {
"_id" : 2,
"city" : "CityB",
"loc" : [
56.02,
11.02
]
}
}
],
"stats" : {
"time" : 0,
"btreelocs" : 0,
"nscanned" : 2,
"objectsLoaded" : 2,
"avgDistance" : 0.0212132034355966,
"maxDistance" : 0.028292673810819097
},
"ok" : 1
}
>
두 위치가 모두 반환됩니다. 몽고 쿼리와 정규 표현식을 사용하는 방법에 대한 문서는 "고급 검색어 '페이지의"정규 표현식 "섹션에서 찾을 수있다 : 당신의 문제를 해결하고 올바른 쿼리를 수행 할 수 http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions
희망 위.