I have Java Spring boot application with MongoDB database. I’m trying to search for the keyword inside the collection with sub-document using the MongoOperations Query Criteria, without specifying the fieldname in the sub-document.
Basically, I want to do full sub-document search for the value across all fields and return the List of document objects.
I have tried multiple options using Query Criteria but northig works. Sample code is as below.
Code 1:
var query = new Query();
query.addCriteria(Criteria.where("excelDataList")
.elemMatch(Criteria.where("")
.elemMatch(Criteria.where("data")
.regex(keyword, "i"))));
List<ExcelInfo> excelInfoList = this.mongoOperations.find(query,ExcelInfo.class);
Code 2:
var query = new Query();
query.addCriteria(Criteria.where("excelDataList")
.elemMatch(Criteria.where("")
.elemMatch(Criteria.where("data")
.elemMatch(Criteria.where("$**")
.regex(keyword, "i")))));
List<ExcelInfo> excelInfoList = this.mongoOperations.find(query,ExcelInfo.class);
Please suggest how we can achieve sub-document search across all fields in the collection.
Sample Message:-
search Keyword in the message:- RatesSystem
[{
"_id": {
"$oid": "66f31e018038282a16ff2e75"
},
"region": "EUROPE",
"system": "FX",
"route": "Inbound",
"feedName": "FXRATES",
"upStreamDownStreamSystem": "RatesSystem",
"versionNo": "1",
"excelName": "RATES_Files.xlsx",
"excelDataList": [
[
{
"data": {
"Countries": "Australia",
"Files": "Australia_Rates_Files.txt",
"Source": "RatesSystem"
},
"sheetName": "FXRATES"
}
]
]
}
Anil Patnagere is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.