Reputation: 1509
How I can extract (find) tags for date 2013-01-14?
db.test1.insert(
{
date: Date("2013-01-14"),
name: "Roma",
tags: [{Python:14,Ruby:10,C:4}]
}
)
I tried extract all info for current date, but even this request do not work:
db.test1.find({date:Date("2013-01-14")})
Upvotes: 1
Views: 98
Reputation: 103475
The mongo shell wrap objects of Date type with the ISODate helper but the objects remain of type Date. So when inserting dates in MongDB, you could use the
ISODate()
constructor which returns a Date object using the ISODate()
wrapper instead of the Date()
method which returns the current date as a string.
When you query, use the new Date()
constructor which returns a Date object using the ISODate() wrapper to get a date object that you can then use in the query, bearing in mind that JavaScript date objects months are zero-based index thus January has the value 0 in the constructor parameter.
Inserting:
db.test1.insert({
"date": ISODate("2013-01-14"),
"name": "Roma",
"tags": [
{ "Python": 14, "Ruby": 10, "C": 4 }
]
})
Querying:
var myDateObj = new Date(2013, 0, 14) // JavaScript Date object months are zero based index
db.test1.find({ "date": myDateObj }, {"_id": 0, "tags": 1})
Result:
/* 0 */
{
"tags" : [
{
"Python" : 14,
"Ruby" : 10,
"C" : 4
}
]
}
Upvotes: 1