Search code examples
javamongodbmongodb-querymongotemplate

How to make a query in mongo (mongo template) that it returns a result without alterations?


I have only one document in my mongoDB. This is:

{
"_id": "32fqwefr",
"propertyName": "configShowableField",
"Applicant": [
    "R:a_FII",
    "R:a_PN"
],
"Employment": [
    "R:a_FIIq",
    "R:a_PsN"
],
"Product": [
    "R:a_FII",
    "R:a_PN"
],
"Address": [
    "R:a_FII",
    "R:a_PN"
]
}

and returns that document.

I've made a query to get it

Map<String, List<String>> configShowable = new HasMapsh<>();
Query query = new Query();
query.addCirteria(Criteria.where("propertyName").is("configShowableField"));
Map<String. Object> config = getMongoTemplate.findOne(query, Map.class,"Collection");
config.forEach(k,v)->{
if(v instanceof List){
    configShowable.put(k, (List<String>V));
}
}
return configShowable;

The problem is that the configShowable order is altered and I do not need that. I need that I return the same in which was inserted in the data base, in orther words, the Map than was created is in disoreder.

Exmaple:

{
"_id": "32fqwefr",
"propertyName": "configShowableField",
"Address": [
    "R:a_FII",
    "R:a_PN"
],
"Applicant": [
    "R:a_FII",
    "R:a_PN"
],
"Product": [
    "R:a_FII",
    "R:a_PN"
],
"Employment": [
    "R:a_FIIq",
    "R:a_PsN"
]
}

How can I prevent that disorder??


Solution

  • I suggest you use LinkedHaskMap , keeps the keys in the order they were inserted