तो मूल रूप से आपको फ़िल्टर करने की आवश्यकता है। MongoTemplate मोंगोडब के लिए बहुत सारे ऑपरेशन प्रदान करता है, अगर मोंगो टेम्पलेट में कुछ विधियां मौजूद नहीं हैं, तो हम बीसन Document के साथ जा सकते हैं नमूना। उस स्थिति में, इस लेख को आजमाएं:ट्रिक टू गुप्त मोंगो शेल क्वेरी।
असल में आपको एक मोंगो क्वेरी की तरह कुछ चाहिए। $addFields . का उपयोग करना नीचे दिखाए गए तरीकों में से एक। लेकिन आप $project . का उपयोग कर सकते हैं , $set आदि। यहाँ $addFields आपके history_dates को अधिलेखित कर देता है . (यह दस्तावेज़ में नए फ़ील्ड जोड़ने के लिए भी उपयोग करता है)।
{
$addFields: {
history_dates: {
$filter: {
input: "$history_dates",
cond: {
$and: [{
$gt: ["$$this", "23/07/2020"]
},
{
$lt: ["$$this", "24/07/2020"]
}
]
}
}
}
}
}
काम कर रहे मोंगो खेल का मैदान।
आपको इसे स्प्रिंग डेटा में बदलने की जरूरत है। तो @Autowired अपनी कक्षा में MongoTemplate.
@Autowired
MongoTemplate mongoTemplate;
विधि है,
public List<Object> filterDates(){
Aggregation aggregation = Aggregation.newAggregation(
a->new Document("$addFields",
new Document("history_dates",
new Document("$filter",
new Document("input","$history_dates")
.append("cond",
new Document("$and",
Arrays.asList(
new Document("$gt",Arrays.asList("$$this","23/07/2020")),
new Document("$lt",Arrays.asList("$$this","24/07/2020"))
)
)
)
)
)
)
).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());
return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_CLASS.class), Object.class).getMappedResults();
}
Mongo टेम्प्लेट $addFields . के लिए जोड़ने के तरीके प्रदान नहीं करता है और $filter . तो हम बस bson दस्तावेज़ पैटर्न के साथ जाते हैं। मैंने वसंत में इसका परीक्षण नहीं किया है।