开发者

Querying UUID fields in Mongodb using C#

开发者 https://www.devze.com 2023-04-02 06:19 出处:网络
I have some GUIDs stored in my collection in MongoDb. The problem is that I cannot query for them using simple

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开发者_如何学Go OK but I cannot search for them. How should I query then?


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; }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消