Fetch Month from ISODate MongoDB

3,197 views
Skip to first unread message

sh

unread,
Jan 7, 2015, 12:59:13 PM1/7/15
to mongod...@googlegroups.com
Hi,

When i  try to  query something on month for ISODate() 

db.xxx.find({$where: function() { return 'personalInfo.dateOfBirth.getMonth()' == 8 ;}} ); it returns me nothing but when i query as below 

db.xxx.find({ $where: "ISODate(\"1900-09-14T00:00:00Z\").getMonth() == 8"}).count()  i get the count back.

can you please help on how to fetch the month & compare it. the dateOfBirth format is "dateOfBirth" : ISODate("1900-09-14T00:00:00Z")


Thanks
Sh




Will Berkeley

unread,
Jan 7, 2015, 1:31:33 PM1/7/15
to mongod...@googlegroups.com
Don't use $where at all. If you want to query for birthdays in September, use aggregation and $month:

db.xxx.aggregate([
    { "$project" : { "birthMonth" : { "$month" : "$personalInfo.dateOfBirth" } } },
    { "$match" : { "birthMonth" : 9 } }  // note $month returns dates with Jan = 1, not Jan = 0 like .getMonth
])

Ideally, you'd have a $match stage using an index at the start of the pipeline to avoid projecting the birthMonth field for every document. If this is a query you need to do all the time, I'd suggest adding a birthMonth field to the documents (and indexing it), so you can just write something like

db.xxx.find({ "personalInfo.birthMonth" : 9 })

-Will

sh

unread,
Jan 7, 2015, 1:53:50 PM1/7/15
to mongod...@googlegroups.com
thank you will but when i try the below aggregate i am getting following error 

Error: Printing Stack Trace
    at printStackTrace (src/mongo/shell/utils.js:37:15)
    at DBCollection.aggregate (src/mongo/shell/collection.js:897:9)
    at (shell):1:19
Wed Jan  7 18:53:59.666 JavaScript execution failed: aggregate failed: {
        "errmsg" : "exception: can't convert from BSON type EOO to Date",
        "code" : 16006,
        "ok" : 0


 db.xxx.aggregate([{ "$project" : {"birthMonth" : {"$month" : "$personalInfo.dateOfBirth"}}},{ "$match" : { "birthMonth" : 9 } }])

sh

unread,
Jan 7, 2015, 2:26:44 PM1/7/15
to mongod...@googlegroups.com
Got it working some fileds were empty so added another condition to it .

 db.xxx.aggregate([{$match: {"Id":"2408"}},{"$project" : { "birthMonth" : { "$month" : "$personalInfo.dateOfBirth" } } },{ "$match" : { "birthMonth" : 9 } }])


Can you please point out to be why the where clause is not working in my below query.

db.xxx.find({$where: function() { return 'personalInfo.dateOfBirth.getMonth()' == 8 ;}} ); it returns me nothing but when i query as below 






Will Berkeley

unread,
Jan 7, 2015, 2:33:12 PM1/7/15
to mongod...@googlegroups.com
I'd rather not, because you should avoid using $where when there are other options...but the problem is that you need to reference the document using this and not make the body of the return statement be a string:

db.xxx.find({ "$where" : function() { return this.personalInfo.dateOfBirth.getMonth() == 8 } } )

-Will

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/970b7572-1efd-4652-bacf-2e21e8efdf44%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages