Oh, I'm dumb, I just noticed that ISO Dates can be compared for date ranges as strings, so problem solved :)

On Thu, 19 Jun 2014 08:21:59 GMT, Juanjo Alvarez wrote:

Hi!

I'm inserting a moderately complex document into MongoDB (with subdocuments, lists, etc) so I'm creating a string and using insert(parseJsonString(myString)). I'm having problems trying to insert a BsonDate object this way.

I first tried to do (simplified example):

string jsonStr;
auto bsonDate = BsonDate(to!DateTime(Clock.currTime));
jsonStr = format(`{"somefield: "somevalue, "dateobject": %s}`, bsonDate.toString);
mongodb["mycollection"].insert(jsonStr);

This generates this string:

{"somefield": "somevalue", "dateobject": "2014-05-01T21:07:54Z"}

But "dateobject" is inserted as a string into MongoDB. So next I tried:

jsonStr = format(`{"somefield": "somevalue", "dateobject": ISODate("%s")}`), bsonDate.toString);

This time the string is:

{"somefield": "somevalue, "dateobject": ISODate("2014-03-18T14:07:30Z")}

This works with the Mongo console, but the API gives me a runtime error:

std.json.JSONException@std/json.d(804): JSON format error at byte 15: Expected valid json token, got '32ISODate("201'.

Then I thought about inserting the document and doing an update, but if I use update() with an array it unset all the other fields but if I try to use a json String to use $set I've the same problem.

How could I do this?

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?