Reputation: 34396
I have set up my database so that the ID property of my class is the ID of the document:
BsonClassMap.RegisterClassMap<TestClass>(cm =>
{
cm.AutoMap();
cm.SetIdMember(cm.GetMemberMap(c => c.ID));
});
To query for a specific document, I can successfully use:
collection.FindOneById(123);
However if I try the equivalent as a query:
collection.FindOne(Query.EQ("ID", 123));
No results are returned.
How do I include the document ID as part of the query and/or how do I query for a list of documents matching a given set of IDs? Do I have to literally query them one by one?
EDIT To clarify, I'm not talking about an internal MongoDB-assigned object ID, I'm talking about the ID assigned by the user as the unique key/id identifying that document for later retrieval.
Upvotes: 1
Views: 1366
Reputation: 30695
If you're talking about the internal ID that MongoDB uses, it's specifically a BsonObjectId
and needs to be queried as one. Also, the ID field for MongoDB documents is lowercase and starts with an underscore: _id
Code like this should work:
collection.FindOne(Query.EQ("_id", ObjectId.Parse("4dad901291c2949e7a5b6aa8")));
Upvotes: 2