C# MongoDB Guid Problem
-
I'm learning MongoDB. I'm working on a Repository implementation for both MongoDB and LinqToSQL. My MongoRepository Add method is
//T is a StudentEntity
public override void Add(T entity)
{
var collection = GetCollection(CollectionName);
collection.InsertOne(entity);
}This works fine. This writes out
_id:Binary('aE/9nTBrM0mkDMf6/GpSCg==')
FirstName:"John"
LastName:"Smith"
Class:"English 101"
Age:32to the Students collection. Then, my Get method has:
public override T Get(Guid id)
{
var collection = GetCollection(CollectionName);
var result = collection.Find(r => r.Id == id).FirstOrDefault();
return result;
}and I call it like this
private static void GetStudent()
{
var student = _repository.Get(new Guid("aE/9nTBrM0mkDMf6/GpSCg==")); //<=== THROWS
Console.WriteLine(student);
}The exception is "Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).'" It seems that Mongo stores the Guid differently than in .Net. What's the right way to fix this? Not sure how to handle this.
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
I'm learning MongoDB. I'm working on a Repository implementation for both MongoDB and LinqToSQL. My MongoRepository Add method is
//T is a StudentEntity
public override void Add(T entity)
{
var collection = GetCollection(CollectionName);
collection.InsertOne(entity);
}This works fine. This writes out
_id:Binary('aE/9nTBrM0mkDMf6/GpSCg==')
FirstName:"John"
LastName:"Smith"
Class:"English 101"
Age:32to the Students collection. Then, my Get method has:
public override T Get(Guid id)
{
var collection = GetCollection(CollectionName);
var result = collection.Find(r => r.Id == id).FirstOrDefault();
return result;
}and I call it like this
private static void GetStudent()
{
var student = _repository.Get(new Guid("aE/9nTBrM0mkDMf6/GpSCg==")); //<=== THROWS
Console.WriteLine(student);
}The exception is "Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).'" It seems that Mongo stores the Guid differently than in .Net. What's the right way to fix this? Not sure how to handle this.
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
CodeProject is your friend: "3 Best Practices for GUID data in MongoDB" [^]
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
-
I'm learning MongoDB. I'm working on a Repository implementation for both MongoDB and LinqToSQL. My MongoRepository Add method is
//T is a StudentEntity
public override void Add(T entity)
{
var collection = GetCollection(CollectionName);
collection.InsertOne(entity);
}This works fine. This writes out
_id:Binary('aE/9nTBrM0mkDMf6/GpSCg==')
FirstName:"John"
LastName:"Smith"
Class:"English 101"
Age:32to the Students collection. Then, my Get method has:
public override T Get(Guid id)
{
var collection = GetCollection(CollectionName);
var result = collection.Find(r => r.Id == id).FirstOrDefault();
return result;
}and I call it like this
private static void GetStudent()
{
var student = _repository.Get(new Guid("aE/9nTBrM0mkDMf6/GpSCg==")); //<=== THROWS
Console.WriteLine(student);
}The exception is "Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).'" It seems that Mongo stores the Guid differently than in .Net. What's the right way to fix this? Not sure how to handle this.
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Kevin Marois wrote:
"aE/9nTBrM0mkDMf6/GpSCg=="
That's a Base64 encoded string containing the Guid bytes, so:
new Guid(Convert.FromBase64String("aE/9nTBrM0mkDMf6/GpSCg=="))
which returns:
9dfd4f68-6b30-4933-a40c-c7fafc6a520a
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer