inserting with current date

36 views
Skip to first unread message

Kelvin Chua

unread,
Jul 1, 2015, 9:33:08 PM7/1/15
to mongod...@googlegroups.com
i am using mongo c driver.

i am able to insert a document by doing this

{ "insert": "cdrs", "documents": [{"time": "1435702850703.563"}] }

what i want to do is store it as a date object. new Date() does not work. i would get a 
lexical error: invalid char in json text

how should i do this?

Alberto Frosi

unread,
Jul 3, 2015, 4:22:47 AM7/3/15
to mongod...@googlegroups.com
Hi Kelvin,
I don't know if this could help you but in my python programs to insert current date I use "data" :datetime.now()
In the past but I don't remember very well, sorry, I had similar issue.
Ciao

Derick Rethans

unread,
Jul 3, 2015, 7:05:42 AM7/3/15
to mongod...@googlegroups.com
https://www.google.com/?gws_rd=ssl#q=mongodb+insert+current+date

Leads me to:
http://docs.mongodb.org/manual/reference/operator/update/currentDate/

Example:

> db.test.update( {}, { $set: { title: "test", topic: "Testing current date" }, $currentDate: { date: true } }, { upsert: true } );
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("55966b9df4a523ae7b09d0be")
})
> db.test.find()
{ "_id" : ObjectId("55966b9df4a523ae7b09d0be"), "title" : "test", "topic" : "Testing current date", "date" : ISODate("2015-07-03T11:01:49.665Z") }

cheers,
Derick

--
{
website: [ "http://mongodb.org", "http://derickrethans.nl" ],
twitter: [ "@derickr", "@mongodb" ]
}
Reply all
Reply to author
Forward
0 new messages