अगर मैं एक दस्तावेज के रूप में परिभाषित किया
[
{
"title": "title",
"tags": [ "cool", "amazing", "funny" ]
},
{
"title": "another title",
"tags": [ "nice", "amazing", "funny" ]
}
]
मैं करना चाहते हैं के लिए सक्षम होना करने के लिए क्वेरी के साथ MongoTemplate पारित करने के क्रम में मानों की एक सूची की तरह ["शांत","अद्भुत"] और बदले में पहले संग्रह से ऊपर, दूसरा नहीं. के लिए मैं क्या मतलब है को प्राप्त करने में$ हालत नहीं लगता है पर्याप्त. मैं के साथ की कोशिश की $सभी की हालत और मोंगो कंसोल यह काम करता है के रूप में मैं की जरूरत है, लेकिन मेरे कोड में कुछ काम नहीं कर रहा है और यह हमेशा के लिए लेता है के लिए क्वेरी करने के लिए विस्तृत है । के साथ $में ऑपरेटर अपने कोड तेजी से चला जाता है के बजाय । मेरे विधि में भंडार (अन्य कारणों के लिए, मैं क्या करने के लिए एक एकत्रीकरण नीचे के रूप में):
public Page<MyDocument> findByProperties(String title, List<ObjectId> tags, Pageable page) {
final List<Criteria> criteria = new ArrayList<>();
Aggregation aggregation = null;
if (title != null && !title.isEmpty()) {
criteria.add(Criteria.where("title").is(title));
}
if (tags != null && !tags.isEmpty()) {
criteria.add(Criteria.where("MyBook.tags").all(tags));
}
if (!criteria.isEmpty()) {
aggregation = Aggregation.newAggregation(
Aggregation.lookup("from_collection", "_id", "idParent", "MyBook"),
Aggregation.unwind("MyBook"),
Aggregation.match(new Criteria().andOperator(criteria.toArray(new Criteria[0]))),
Aggregation.skip(page.getOffset()),
Aggregation.limit(page.getPageSize()),
Aggregation.sort(page.getSort())
);
} else {
aggregation = Aggregation.newAggregation(
Aggregation.lookup("from_collection", "_id", "idParent", "MyBook"),
Aggregation.unwind("MyBook"),
Aggregation.skip(page.getOffset()),
Aggregation.limit(page.getPageSize()),
Aggregation.sort(page.getSort())
);
}
List<MyDocument> results = mongoTemplate.aggregate(aggregation, "my_document", MyDocument.class).getMappedResults();
return PageableExecutionUtils.getPage(results, page,
() -> (long)results.size());
}
देख यह जवाब है, मैं के साथ की कोशिश की
criteria.add(Criteria.where("MyBook.tags").in(tags).all(tags));
लेकिन कुछ भी नहीं बदला है, क्वेरी लेता है हमेशा के लिए नहीं है और उम्मीद उत्पादन के. किसी भी विचार है, कृपया? धन्यवाद!