Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using MongoDB ObjectId as id #24

Closed
markiodev opened this issue Apr 21, 2015 · 1 comment
Closed

Using MongoDB ObjectId as id #24

markiodev opened this issue Apr 21, 2015 · 1 comment

Comments

@markiodev
Copy link

Hi Mauricio,

Thanks for this library, I'm enjoying using it at the moment. We're looking at using MongoDB as our main database system, while also using another embedded solution (maybe LiteDB) for the desktop version of our software.

Do you think it's possible for LiteDB to use the same ObjectId as MongoDB, so that we can re-use one C# class for both storage types?

Thanks,
Mark.

@mbdavid
Copy link
Owner

mbdavid commented Apr 21, 2015

Hey Mark, thanks for using LiteDB. It's really nice to know.... 😄

LiteDB.ObjectId is almost the same class than MongoDB.Bson.ObjectId. To use MongoDB.Bson.ObjectId as you Id, just write a small custom serialize/deserialize functions to do this:

LiteDB.BsonMapper.Global.RegisterType<MongoDB.Bson.ObjectId>
(
    (oid) => new LiteDB.ObjectId(oid.ToByteArray()),
    (bson) => new MongoDB.Bson.ObjectId(bson.AsObjectId.ToByteArray())
);

@mbdavid mbdavid closed this as completed Apr 25, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants