2011-09-28 6 views
1

가 어떻게 MongoDB를위한 10gen의 C#을 드라이버를 사용하여 Geospatial Haystack Index를 만들려면 어떻게해야합니까MongoDB를 지리 건초 더미 인덱싱

JS 예 :

db.foo.ensureIndex({ pos : "geoHaystack", type : 1 }, { bucketSize : 1 }) 

C# 예 작동하지 않습니다 :

BsonDocument keys = new BsonDocument(); 
keys.Add("pos", "geoHaystack"); 
keys.Add("type", "1"); 

IMongoIndexKeys indexKeys = new IndexKeysDocument(keys); 

IndexOptionsDocument indexOptions = new IndexOptionsDocument("bucketSize", new BsonInt32(1)); 

collection.CreateIndex(indexKeys, indexOptions); 

이를 제공 오류 :

MongoDB.Driver.MongoSafeModeException : Safemode detected an error 'can only have 1 index plugin/bad index key pattern'. (Response was { "err" : "can only have 1 index plugin/bad index key pattern", "code" : 13007, "n" : 0, "connectionId" : 6, "ok" : 1.0 }).

나는이 같은 '유형'키를 제거하면 63,210

그래서 :

IMongoIndexKeys keys = new IndexKeysDocument { 
    { "Position", "geoHaystack" }, 
    { "type", 1 } 
}; 

IMongoIndexOptions options = new IndexOptionsDocument { 
    { "bucketSize", 1 } 
}; 

collection.EnsureIndex(keys, options); 

문제 I : 내가 작업을 얻었다

MongoDB.Driver.MongoSafeModeException : Safemode detected an error 'no other fields specified'. (Response was { "err" : "no other fields specified", "code" : 13317, "n" : 0, "connectionId" : 7, "ok" : 1.0 }).

답변

1

keys.Add("type", 1); 

를 정수로 예상대로 작동합니다.

+0

감사합니다. RameshVel :) – Robert

1

:

BsonDocument keys = new BsonDocument(); 
keys.Add("pos", "geoHaystack"); 

IMongoIndexKeys indexKeys = new IndexKeysDocument(keys); 

IndexOptionsDocument indexOptions = new IndexOptionsDocument("bucketSize", new BsonInt32(1)); 

collection.CreateIndex(indexKeys, indexOptions); 

을 나는이 오류 이미 컬렉션에로드 된 데이터와 관련이 있습니다.

그런 다음에 쿼리 할 수 ​​있어야한다 :

가 실제로 문제가 지수 방향이 문자열 값

keys.Add("type", "1"); 

변경으로 지정 하였다

var command = new CommandDocument { 
    { "geoSearch", "foo" }, 
    { "near", new BsonArray { 33, 33 } }, 
    { "maxDistance", 6 }, 
    { "search", new BsonDocument { { "type", "restaurant" } } }, 
    { "limit", 30 } 
}; 
database.RunCommand(command);