I am using Mongo .Net driver to insert documents in MongoDB, and I need to get the documents using Java driver.
My Model:
public class Person{
public Guid Id {get;set;}
public Guid FatherId{get;set;}
public string Name{get;set;}
}
And I am inserting a document to MongoDb using the following C# code.
var id= Guid.NewGuid();
Persons.InsertOne(new Person(){Id = id,Name = "Joe"});
Now, having the id, how can I find the same document using Mongo Java driver? I tried:
Person person=Persons.find(eq("_id", id))).first();
But I am not getting any result, I have researched it and it seems like id should be converted to Base64 before using find.
So I tried the following:
public String uuidToBase64(String str) {
java.util.Base64.Encoder encoder=Base64.getUrlEncoder();
UUID uuid = UUID.fromString(str);
ByteBuffer uuidBytes = ByteBuffer.wrap(new byte[16]);
uuidBytes.putLong(uuid.getMostSignificantBits());
uuidBytes.putLong(uuid.getLeastSignificantBits());
return encoder.encodeToString(uuidBytes.array());
}
Person person=Persons.find(eq("_id", BinData(3,uuidToBase64(id))))).first();
That still did not work.