I had the problem with my query. How can I do something like this SQL query in mongoDB query?
SELECT convert(datetime, field1)
FROM MyTable
I had the problem with my query. How can I do something like this SQL query in mongoDB query?
SELECT convert(datetime, field1)
FROM MyTable
Basicly you cannot do it. While you cannot reference back a value inside a query you cannot convert it on the fly. You can update with a certain value if you know what you likely to put in. There is no such thing:
db.collection.update({},{$set:{b:ISODate('$b')}})
This is a current enhancement request.
The only way is to write a script which crawl through the collection save docs with the updated type . You also can run query based on the type of a field which may help if your flow brakes in between.
For this solution check that question : MongoDB: How to change the type of a field?
The solution there is something like :
db.collection.find().forEach( function (x) {
x.b = new ISODate(x.b); // convert field to ISODate
db.collection.save(x);
});
You can't. If you need to convert a value stored in MongoDB, the only way to do it is in your application code. If this is a conversion that you need to do often and/or if you need to run queries on the converted values, you should consider storing both the original value and the converted value in the document. Denormalizations like this are very common in MongoDB schema design.