-1

I inserted a document in MongoDB like:

db.test.insert({date:20150101});

When I tried to use db.test.find({date:"20150101"}) to search it (date string with double quote), no results were returned.

Only when I use db.test.find({date:20150101}), it returns a result. It seems MongoDB will strictly compare the date value.

Is there any way that I can search it with either double quote version or numeric version?

chridam
  • 100,957
  • 23
  • 236
  • 235
Kuan
  • 11,149
  • 23
  • 93
  • 201
  • does 20150101 means 1 jan 2015? – codeofnode May 20 '15 at 19:28
  • @codeofnode Yes. I wonder if you can help me with two things: [1] search Date with String-type Date or Numeric-type Date. [2] Find the range of the date in the search result. – Kuan May 20 '15 at 20:34
  • for [1] refer my answer, i did not get yours [2].. what range you are talking about – codeofnode May 20 '15 at 20:38
  • @codeofnode thanks, I did not quite understand how [1] works, but I will figure out later. For [2], since we may get more than one results( my date search example may be not proper, cos it seems it has been nailed down to a single day but it may varied at Hour and Minute or we may use $gt $lt etcs, so let me use other field as example, let us say the field "price", and we search all price greater than certain value), how can I get the range among those results? – Kuan May 20 '15 at 20:43
  • as far as i imagine, you do need to write scripts rather than just firing commands, after finding the results simply find the min price and max price and print results. you may refer http://docs.mongodb.org/manual/tutorial/write-scripts-for-the-mongo-shell/ for how to write scripts in mongo db – codeofnode May 20 '15 at 20:51
  • You've inserted the date as a number (`20150101`) which won't match if you search for it as a string (`"20150101"`). I'd suggest what you **really** want to do is insert it as a proper [`Date` type](http://docs.mongodb.org/manual/core/shell-types/#date) in MongoDB: `db.test.insert({date:new Date('2015-01-01')})`. With a real `Date` type you can do useful server-side manipulation like using range comparisons and [date aggregation operators](http://docs.mongodb.org/manual/reference/operator/aggregation-date/). – Stennie May 21 '15 at 07:23

2 Answers2

0

I would cast the returning result to a string, and then compare it with the given value with your favourite programming language.

Note that in {date:20150101}, the value is actually an integer rather than a date.

Pierre Prinetti
  • 9,092
  • 6
  • 33
  • 49
  • Thanks, right now, I do not have control to original data( the document stored in MongoDB is by others). Ok, let us forget current data issue, suppose I have control to the data and I can start it over right now, how should I handle(store it as a Date type?) the date data, I may need do a range search in the future. – Kuan May 20 '15 at 19:28
  • For date format in mongodb, refer to [this question](https://stackoverflow.com/questions/3778428/best-way-to-store-date-time-in-mongodb). Please consider accepting my answer if it solves your problem :) – Pierre Prinetti May 20 '15 at 19:31
  • well, could you tell me how do you cast the return result into string? I did not know how to do that i MongoDB – Kuan May 20 '15 at 20:35
0

You may fire a query like this

mongo test --eval "function convert(d){  return new Date(d.substring(0,4)+'/'+d.substring(4,6)+'/'+d.substring(6,8)); }; var c = db.test.find({ a : convert('20150101') }); while(c.hasNext()){ printjson(c.next());  }"

Just replace '20150101' as parameter of the convert call with the string date you desire

codeofnode
  • 18,169
  • 29
  • 85
  • 142