[英]MongoDB : Find/filter data by today date of birth without aggregation
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.
我将数据存储在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.
到目前为止,我正在根据用户给出的规则动态将mongo查询(java)构建为BasicDBObject。
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). 因此,过滤器需要像$ date.month = 12 && $ date.day = 10这样发生(以获取其出生日期在12月10日以下的所有记录)。
Can some one help how it can be achieved in Mongo query? 有人可以帮助在Mongo查询中实现它吗?
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();
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: 尽管如此,没有它,由于没有用于“常规”查询的日期运算符,因此您实际上有三个选择:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.