Skip to content
This repository has been archived by the owner on Feb 22, 2020. It is now read-only.

Certain value types serializing as binary? #45

Open
dasjestyr opened this issue Jan 9, 2017 · 3 comments
Open

Certain value types serializing as binary? #45

dasjestyr opened this issue Jan 9, 2017 · 3 comments
Assignees
Labels

Comments

@dasjestyr
Copy link

So I'm just trying this out for the first time and I noticed that types like Guid and DateTimeOffset were getting serialized as binary blobs and/or byte arrays. Is that normal? I would have thought they would have called ToString() before storing or something.

@RobThree
Copy link
Owner

RobThree commented Jan 9, 2017

You can use an attribute (for example: a BsonDateTimeOptions attribute) to change the serializiation to your needs (in this example the attribute can be used to specify you would only want to serialize the date-'part' for example and ignore the time-'part'). This is not something MongoRepository provides but is provided by the underlying mongo-csharp-driver. You can find documentation here. You can also use conventions (see here) which are, again, something provided by the underlying mongo-csharp-driver.

I would, however, advice to use the 'binary blob' unless you have a very specific and good reason to want to serialize to string.

@RobThree RobThree self-assigned this Jan 9, 2017
@RobThree RobThree changed the title QUESTION: Certain value types serializing as binary? Certain value types serializing as binary? Jan 9, 2017
@dasjestyr
Copy link
Author

dasjestyr commented Jan 10, 2017

I would, however, advice to use the 'binary blob' unless you have a very specific and good reason to want to serialize to string.

Not sure how I'd query on binary...

I'll look into these other things. Thanks!

@RobThree
Copy link
Owner

RobThree commented Jan 10, 2017

Not sure how I'd query on binary...

If you store an object:

class MyObject : Entity {
    public Guid SomeProperty { get; set; }
}

var myrepo = new MongoRepository<MyObject>();

var someguid = Guid.NewGuid();
var x = new MyObject { SomeProperty = someguid };

// Save object
myrepo.Update(x);

You can simply query it like this:

myrepo.Where(o => o.SomeProperty == someguid);

(Code written on the fly; may contain some (syntax)errors).

If you want to query it from other platforms you can query it with something like:

db.collection.find({ "SomeProperty": new BinData(3, "MSlQ8XxCwE6I9tCqLtXy9g==")})

Where MSIQ....y9g== is the Base64 representation of the GUID; this may differ for other datatypes like datetime which would be something like:

db.collection.find({ "SomeProperty": ISODate("2016-10-20T15:23:11Z")})

But unless you write your queries 'manually' like in the above format MongoRepository (actually the underlying driver) should take care of it.

See also here and here and here for more info.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants