1

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")})

0

1 Answer 1

1

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
        }
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.