简体   繁体   English

MongoDB:按今天的出生日期查找/过滤数据而不进行汇总

[英]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: 尽管如此,没有它,由于没有用于“常规”查询的日期运算符,因此您实际上有三个选择:

  1. You could attempt to use the $where operator which won't be super efficient: https://docs.mongodb.com/manual/reference/operator/query/where/ 您可以尝试使用效率不高的$ where运算符: 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/ ). 日期以Unix时间戳记存储在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). 因此,您可以尝试计算相关的数字或范围,并使用(一堆)长值访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM