Spring MongoDB query documents if days difference is x days

后端 未结 2 1794
無奈伤痛
無奈伤痛 2021-02-19 07:15

I have a collection that has two date fields and I am trying to query all records that have a difference of 15 days:

{
   \"_id\" : \"someid\",
   \"factoryNumbe         


        
2条回答
  •  后悔当初
    2021-02-19 07:46

    Starting 3.6 you have to use new operator $expr which allows use of aggregation expressions inside match queries or in regular queries.

    You can create the json query and pass it directly as $expr is not supported in spring yet in regular query.

    15 days = 15 * 24 * 60 * 60 * 1000 = 1296000000 millis

    Query query = new BasicQuery("{'$expr':{'$gte':[{'$subtract':[{'$ifNull':['$acceptedDate',{'$date':" + System.currentTimeMillis() + "}]},'$lastVisit']},1296000000]}}");
    List results = mongoTemplate.find(query, Document.class);
    

    3.4 version

    If you like to use spring mongo methods you have to use projection to add new field which holds comparison and followed by match operation and extra projection to drop the comparison field. Unfortunately $addFields is still not supported so you have to use the AggregationOperation to create a new stage manually.

    AggregationOperation addFields = new AggregationOperation() {
        @Override
        public Document toDocument(AggregationOperationContext aggregationOperationContext) {
            Document document = new Document("comp", Document.parse("{'$gte':[{'$subtract':[{'$ifNull':['$acceptedDate', {'$date':" + System.currentTimeMillis() + "}]},'$lastVisit']},1296000000]}}"));      
            return new Document("$addFields", document);
        }
    };
    
    Aggregation aggregation = Aggregation.newAggregation(
            addFields,
            Aggregation.match(Criteria.where("comp").is(true))
            Aggregation.project().andExclude("comp");
    );
    
    List results = mongoTemplate.aggregate(aggregation, collection name, Document.class).getMappedResults();
    

    3.2 version

    AggregationOperation redact = new AggregationOperation() {
        @Override
        public DBObject toDBObject(AggregationOperationContext aggregationOperationContext) {
        Map map = new LinkedHashMap<>();
        map.put("if",  BasicDBObject.parse("{'$gte':[{'$subtract':[{'$ifNull':['$acceptedDate', {'$date':" + System.currentTimeMillis() + "}]},'$lastVisit']},1296000000]}}"));
        map.put("then", "$$KEEP");
        map.put("else", "$$PRUNE");
        return new BasicDBObject("$redact", new BasicDBObject("$cond", map));
    };
    
    Aggregation aggregation = Aggregation.newAggregation(redact);
    
    List results = mongoTemplate.aggregate(aggregation, FactoryAcceptance.class, FactoryAcceptance.class).getMappedResults();
    

提交回复
热议问题