Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB C++, How to add ISODate value when inserting

This is about the new MongoDB C++ Driver (not the legacy one). I can insert a document this way:

value Value = document{}
<<"Key" <<"Value"
<<finalize;

cxxClient["db"]["collection"].insert_one(Value.view());

The above code insert a document with 1 field 'Key' of value 'Value'. I can insert string, int, float,... but just can't figure out how to insert ISODate. The new MongoDB C++ Driver should come with more examples in documentation.

like image 931
Dee Avatar asked Nov 25 '16 04:11

Dee


People also ask

What is ISODate in MongoDB?

ISODate() is a helper function that's built into to MongoDB and wraps the native JavaScript Date object. When you use the ISODate() constructor from the Mongo shell, it actually returns a JavaScript Date object.

How does MongoDB store time data?

MongoDB stores times in UTC by default, and will convert any local time representations into this form. Applications that must operate or report on some unmodified local time value may store the time zone alongside the UTC timestamp, and compute the original local time in their application logic.


1 Answers

Thanks Styvane, I found it out how!

value Value = document{}
<<"Key" <<"Value"
<<"Date" <<bsoncxx::types::b_date(std::chrono::system_clock::now())
<<finalize;

cxxClient["db"]["collection"].insert_one(Value.view());
like image 172
Dee Avatar answered Oct 12 '22 00:10

Dee