On Thu, 19 Jun 2014 11:14:20 GMT, Sönke Ludwig wrote:
JSON only supports the basic types
bool
,long
,double
,string
, arrays and dictionaries, so there is no way to represent a date directly with it. To use the full range of BSON types, there are two main ways to go:
I knew about JSON but being new to MongoDB for some reason I tough it would add new types over the JSON basic ones, like a Data type (MongoDB is my first NoSQL experience).
- The fast and memory efficient way is to define a
struct
that matches the data and insert that (it will be directly serialized to BSON):struct MyCollectionEntry { string somefield; SysTime dateobject; // BsonDate works as well } auto entry = MyCollectionEntry("somevalue", Clock.currTime()); mycollection.insert(entry);
See also the "Functions" section of vibe.data.serialization for some attributes that can be used to customize how the data is serialized.
That's pretty cool, I was already organizing most of my data into structs anyway.
Another question: is there any way to get the WriteResult after doing an insertion (or just getting the _id) without doing a call to find() next?
If you use
BsonObjectID
as the_id
, then the best approach is to generate it on the client usingBsonObjectID.generate()
That'll do it.
Thanks for your reply,
Juanjo