1

I am working on the product where user can upload the data and the datatype for the data will be picked up by the user dynamically while uploading. I am storing the data in mongodb. User can write the rule on front end to segment the data. One of the requirement is, the rule can be given such a way that find all the records whose date of birth is today. For that I might need to filter the document by day & month. As of now I am building the mongo query (java) as BasicDBObject dynamically based on rule given by user.

So filter needs to happen like $date.month=12 && $date.day=10 (to get all the records whose birth date fall under 10th of december).

Can some one help how it can be achieved in Mongo query?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Preetham
  • 33
  • 6
  • Possible duplicate of [Mongodb query specific month|year not date](https://stackoverflow.com/questions/18907566/mongodb-query-specific-monthyear-not-date) – dnickless Nov 27 '17 at 21:02
  • I dont want to achieve this with aggregation. The actual process Im doing here is getting all the documents and iterate them by applying filters and projections. Im not using aggregation. – Preetham Nov 28 '17 at 05:45

2 Answers2

0

This is how im iterating through documents. Not any aggregations here.

MongoCollection<Document> mongoCollection = mongoDatabase.getCollection(collectionName);
    FindIterable<Document> iterable;
    iterable = (ruleQuery == null) ? mongoCollection.find().limit(limit).skip(skipCount) : mongoCollection.find(ruleQuery).limit(limit).skip(skipCount);
    iterable = (projectionFieldsQuery == null) ? iterable : iterable.projection(projectionFieldsQuery);
    iterable = (sortQuery == null) ? iterable : iterable.sort(sortQuery);
    return iterable.iterator();
Preetham
  • 33
  • 6
0

There's nothing wrong with using the aggregation framework. I would go as far as saying that sooner or later it is going to be the default for all queries anyway - at least internally...

Nonetheless, without it you've effectively got three options since there are no date operators for "normal" queries:

  1. You could attempt to use the $where operator which won't be super efficient: https://docs.mongodb.com/manual/reference/operator/query/where/
  2. You could store your date part information (perhaps additionally) in individual fields and simply use them in your queries. These fields could be indexed so this would be fast.
  3. Dates are stored as Unix timestamps in MongoDB (https://docs.mongodb.com/manual/reference/method/Date/). So you could attempt to calculate the relevant numbers or ranges and access them using a (bunch of) long value(s).
dnickless
  • 10,733
  • 1
  • 19
  • 34