0

I have below document in Mongo DB and I wrote a java code to get data from innermost element . For some reason its not returning me any results for it.

Input data

{
"_id": "59036b0fa036cc28c8e07db6",
"srcName":"test1",
"sections": [{
    "_id": "8769669696",
    "data": [{
            "srcKey": "Bonds",
            "rowIdx": 0,
            "values": [{
                    "srcDesc": "Assets",
                    "valuesNumber": 10000
                },
                {
                    "srcDesc": "NonAssets",
                    "valuesNumber": 75500
                },
                {
                    "srcDesc": "liabilities",
                    "valuesNumber": 1566
                }
            ]
        },
        {
            "srcKey": "01",
            "rowIdx": 1,
            "values": [{
                "srcDesc": "NonAssets",
                "valuesNumber": 1566
            }]
        }
    ]
}]
}

The result I want is select valuesNumber from...where srcName="test1" AND srcKey="Bonds" AND srcDesc="Assets"

Java code is as below

 AggregationOperation match=Aggregation.match(Criteria.where("srcName").in("test1")
            .and("sections.data.values.srcDesc").in("Assets")
            .and("sections.data.srcKey").in("Bonds"));


    AggregationOperation unwind1=Aggregation.unwind("sections");
    AggregationOperation unwind2=Aggregation.unwind("sections.data");
    AggregationOperation unwind3=Aggregation.unwind("sections.data.values");

    Aggregation aggregation=Aggregation.newAggregation(match,unwind1,unwind2,unwind3,match);

    BasicDBObject basicDBObject=mongoTemplate.aggregate(aggregation,"InsStatData",BasicDBObject.class).getUniqueMappedResult();
athenatechie
  • 699
  • 2
  • 8
  • 15
  • You're referencing srcName under sections in one part and section.data. One of the two is wrong. I'm guessing you need to reference sections.srcName not just srcName. Also, you are referencing values.srcDesc... which is an ambiguous reference (all three are named srcDesc). It is possibly worth noting that you might want to use Spring Data for this. Would make this significantly less complex for you to populate a DTO object and use plain java to get what you want. – Araymer May 09 '17 at 16:53
  • @Araymer My appologies, srcName is outside section. Its along with _id field. – athenatechie May 09 '17 at 17:23
  • I just ran your code for the sample document and I got the `valuesNumber` as 10000. Your code looks good to me. How are you verifying the results ? – s7vr May 09 '17 at 17:59
  • @Veeram, I am testing it on debug mode and checking the value of basicDBObject. BasicDbObject is showing as NULL. – athenatechie May 09 '17 at 18:50
  • Can you try without including the first matching stage ? Also, you may want to add `preserveNullAndEmptyArrays` option for unwind. Something like `AggregationOperation unwind1=Aggregation.unwind("sections", true);` for each of the unwind option to make sure you are not dropping the documents while unwinding. – s7vr May 09 '17 at 18:55
  • It resolved my question. I just had to do a proper mapping to get Mapped results. that was not there in original code. – athenatechie May 10 '17 at 15:37

1 Answers1

0

It resolved my question. I just had to do a proper mapping to get Mapped results. that was not there in original code. – user3516787 just now edit

athenatechie
  • 699
  • 2
  • 8
  • 15