I have just started using MongoDB with Matlab (R2021b). Currently, I am using the mongoc class (https://ch.mathworks.com/help/database/ug/mongoc.html) to connect to my MongoDB database.
I am having issues querying on the date. I have the following collection:
{
"_id": 1,
"stock": "Stock1",
"value": 10,
"date": "2010-01-01T00:00:00.000Z"
},
{
"_id": 2,
"stock": "Stock1",
"value": 20,
"date": "2015-01-01T00:00:00.000Z"
},
{
"_id": 3,
"stock": "Stock1",
"value": 30,
"date": "2020-01-01T00:00:00.000Z"
}
And the end result would need to look as follows:
{
"_id": 2,
"stock": "Stock1",
"value": 20,
"date": "2015-01-01T00:00:00.000Z"
},
{
"_id": 3,
"stock": "Stock1",
"value": 30,
"date": "2020-01-01T00:00:00.000Z"
}
To do this, I have tried in Matlab:
conn = mongoc('localhost', 27017, database_name);
query = '{"date": {"$gte": ISODate("2015-01-01"), "$lte": ISODate("2020-01-01")}}';
res = find(conn, collection_name, 'Query', query);
However, I am getting the following error:
[Mongo Driver Error]: Got parse error at "S", position 19:
"SPECIAL_EXPECTED".
I have tried various ways of placing the quotation marks but none have proven successful. Querying with a similar string in MongoDB Compass however works. Has anyone overcome this issue or is the connector just not able to query on dates? Another solution would be to store the date in another format (int or float) and query on that, but I would like to avoid this kind of solution.
Any help would be greatly appreciated!