Question

I have some GUIDs stored in my collection in MongoDb. The problem is that I cannot query for them using simple

Query.EQ("GuidField", Guid)

They are stored OK but I cannot search for them. How should I query then?

Was it helpful?

Solution

Hmm, try to debug following example to see that it is works:

var _mongoServer = MongoServer.Create(
     MongoUrl.Create("mongodb://admin(admin):1@orsich-pc:27020"));

var database = _mongoServer.GetDatabase("StackoverflowExamples");
var collection = database.GetCollection("guids");

var guid = Guid.NewGuid();
var item = new Item()
   {
     Id = ObjectId.GenerateNewId().ToString(),
     GuidField = guid
   };

collection.Insert(item);

var itemFromDb = collection.FindOneAs<Item>(Query.EQ("GuidField", guid));

Item class:

public class Item
{
  [BsonId]
  public string Id { get; set; }
  public Guid GuidField { get; set; }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top