Spring Data MongoDB Lookup with Pipeline Aggregation

前端 未结 3 460
礼貌的吻别
礼貌的吻别 2020-12-09 12:20

How would I convert the following MongoDB query into a query to be used by my Java Spring application? I can\'t find a way to use pipeline with the provided loo

3条回答
  •  一整个雨季
    2020-12-09 12:54

    The drivers are pretty much always a little bit behind the current language features that MongoDB provides - hence some of the latest and greatest features are simply not nicely accessible through the API yet. I am afraid this is one of those cases and you'll need to resort to using strings. Kind of like so (untested):

    AggregationOperation match = Aggregation.match(Criteria.where("dayOfWeek").is("SOME_VARIABLE_STRING_1"));
    AggregationOperation match2 = Aggregation.match(Criteria.where("deliveryZipCodeTimings").ne([]));
    String query = "{ $lookup: { from: 'deliveryZipCodeTiming', let: { location_id: '$fulfillmentLocationId' }, pipeline: [{ $match: { $expr: { $and: [ { $eq: ['$fulfillmentLocationId', '$$location_id']}, { $eq: ['$zipCode', 'SOME_VARIABLE_STRING_2']} ]} } }, { $project: { _id: 0, zipCode: 1, cutoffTime: 1 } }], as: 'deliveryZipCodeTimings' } }";
    Aggregation.newAggregation(match, (DBObject) JSON.parse(query), match2);
    

提交回复
热议问题