I end doing this and is working for my needs right now, cheers !
protected void postRecordToCollection(MongoClient mongodb, HTTPServerResponse res, string the_id,
const string collection_name, ref string[string] record, bool withTimestamp=false)
{
auto collection = mongodb.getCollection(collection_name);
foreach(k; record.keys)
{
record[k] = record[k].strip;
}
try
{
auto brec = serializeToBson(record);
if(the_id.length > 0)
{
if(withTimestamp)
{
brec["mdate"] = BsonDate(Clock.currTime(UTC()));
}
auto query = Bson.emptyObject;
query["$set"] = brec;
/*
if(withTimestamp)
{
query["$currentDate"] = BsonTimestampField;
}
logInfo("query: %s", query.toString);
*/
collection.update(["_id":Bson(BsonObjectID.fromString(the_id))], query);
} else {
if(withTimestamp)
{
auto now = BsonDate(Clock.currTime(UTC()));
brec["mdate"] = now;
brec["cdate"] = now;
}
collection.insert(brec); //actually there is no way to check if insert fails due to duplicated key or other reasons
}
}
catch(Throwable e)
{
logInfo("Error: %s", e.msg);
}
if(res) {
res.writeBody(format(q{{"ok":"The record was %s !"}}, the_id.length > 0 ? "updated" : "inserted"), "application/json");
}
}