0

I'm trying to use Spring Data MongoDB Aggregation Framework Support to filter arrays based on specified conditions, but I could only get exception:

org.springframework.data.mapping.PropertyReferenceException: No property 10 found for type List! Traversed path: StudyRecord.channelRecords.

I guess what complicates the problem is that those arrays are value type in a map whose key is id of some other table.

The MongoDB document structure looks like this:

{
    "_id" : "dJ_o8070Sq2f8JqhYBZSjqDhP7Wk",
    "channelRecords" : {
        "10" : [ 
            {
                "startTime" : ISODate("2019-04-24T01:31:27.302Z"),
                "endTime" : ISODate("2019-04-24T02:36:27.302Z"),
                "_class" : "StudyRecordSingleDay"
            }, 
            {
                "startTime" : ISODate("2019-04-25T03:01:28.198Z"),
                "endTime" : ISODate("2019-04-25T04:01:28.198Z"),
                "_class" : "StudyRecordSingleDay"
            }
        ],
        "11" : [
            {
                "startTime" : ISODate("2019-04-23T03:01:28.198Z"),
                "endTime" : ISODate("2019-04-23T04:01:28.198Z"),
                "_class" : "StudyRecordSingleDay"
            }
        ]
    }
}

The corresponding POJO class is (class annotations omitted)

public class StudyRecord {

    @Id
    private String someId;

    private Map<Integer, List<StudyRecordSingleDay>> channelRecords;

}

To demonstrate that the aggregation works in mongo shell, here

db.studyRecord.aggregate([
    { $project: {
        "channelRecords.10": {
            $filter: {
                input: "$channelRecords.10",
                as: "record",
                cond: {$gte: ["$$record.studyTime",  ISODate("2019-04-01T23:14:19.000Z")]}
            }
        }
    } }
]);

The ProjectionOperation is constructed as below

project("_id")
.and(ArrayOperators.arrayOf("$channelRecords.10")
    .filter().as("record").by(
        new Document(
           "$gte", 
           Arrays.asList("$$record.startTime", LocalDateTime.parse("2019-04-24T01:31:27"))
        )
    ))
    .as("channelRecords.10")

I could not find specific sample code neither in Spring Data MongoDB reference documentation nor in official test classed.

I debugged some code and found that the problem seems to be caused by the array path reference ArrayOperators.arrayOf("$channelRecords.10"). Spring Data MongoDB seems to regard 10 as a property of channelRecords map's value type, which is List. Therefore, the exception No property 10 found for type List is raised

Would anyone shed light on my problem? Thanks!

Versions: spring-boot-starter-data-mongodb is used with Spring Boot Starter Parent version 2.1.4.RELEASE

2
  • Use match query as given in this stackoverflow question: stackoverflow.com/questions/31643109/… Commented Apr 30, 2019 at 11:46
  • @prakharjain Thanks! The issue I have seems to be in Spring Data section. Unfortunately, the question to which you referred is talking about MongoDB Java driver, while I have to adhere to Spring Data MongoDB framework. Commented May 5, 2019 at 7:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.