MongoDB C# 드라이버를 찾을 수 없음 객체 ID?
문제
MongoDB C# 드라이버 사용 (http://github.com/samus/mongodb-csharp), 객체에 의해 데이터를 얻을 수없는 것 같습니다. 내가 사용하는 명령 아래 :
var spec = new Document { { "_id", id } };
var doc = mc.FindOne(spec);
나는 또한 이것을 시도했다 :
var spec = new Document { { "_id", "ObjectId(\"" + id + "\")" } };
var doc = mc.FindOne(spec);
둘 다 아무것도 반환하지 않습니다. 한편, 몽고 콘솔에서 쿼리하면 예상 결과를 반환합니다.
내 질문은, 그 운전자가 실제로 ObjectId의 조회를 지원합니까?
감사..
해결책
객체 ID에 의한 페치를 지원합니다. ID 변수는 OID 여야합니다. 올바른 유형입니까?
다음은 완전한 프로그램입니다
- 몽고에 연결하십시오
- 문서를 삽입하십시오
- ID를 사용하여 문서를 다시 가져 오십시오
- 문서의 세부 사항을 인쇄하십시오.
// Connect to Mongo
Mongo db = new Mongo();
db.Connect();
// Insert a test document
var insertDoc = new Document { { "name", "my document" } };
db["database"]["collection"].Insert(insertDoc);
// Extract the ID from the inserted document, stripping the enclosing quotes
string idString = insertDoc["_id"].ToString().Replace("\"", "");
// Get an Oid from the ID string
Oid id = new Oid(idString);
// Create a document with the ID we want to find
var queryDoc = new Document { { "_id", id } };
// Query the db for a document with the required ID
var resultDoc = db["database"]["collection"].FindOne(queryDoc);
db.Disconnect();
// Print the name of the document to prove it worked
Console.WriteLine(resultDoc["name"].ToString());
다른 팁
var spec = 새 문서 {{ "_id", objectId.parse (id)}};
var doc = mc.findone (사양);
제휴하지 않습니다 StackOverflow