2

Mongo document 'History':

@Document(collection = "histories")
@Data
public class History {
    @Id
    private String id;

    @Transient
    private final String name = MAIN_FOLDER;
    private String appleId;
    private ArrayList<Item> historyItems = new ArrayList<>();
    private ArrayList<Folder> historyFolders = new ArrayList<>();
}

Model of Folder:

@Data
public class Folder {
    private String name;
    private LocalDateTime date;
    private ArrayList<Item> historyItem = new ArrayList<>();
}

Query with Criteria:

    public Folder findFolderByName(final String name) {
        Query query = new Query();
        query.addCriteria(Criteria.where("historyFolders.name").is(name));
        return template.findOne(query, Folder.class);
    }

I got Null from this query. Sorry, I am new to this technologies, I did'nt find info about Criterias for documented arrays.

Mykola
  • 118
  • 1
  • 14

1 Answers1

0

You are executing the Query at the wrong collection. Folder is not the collection but only a nested document in the "histories" collection.

And also: you cannot just return the nested document with 'classic' query execution. You have to filter it on the client side (your java code), use projections $elemMatch filter and unwind on client-side or use aggregation pipelines for this. Look at this Question: there are a lot of solutions if you need this in a query/pipeline.

Your query looks correct. You only have to update your query execution to use the History Entity instead of the Folder entity.

public History findFolderByName(final String name) {
    Query query = new Query();
    query.addCriteria(Criteria.where("historyFolders.name").is(name));
    return template.findOne(query, History.class);
}

If you use this query often and there a lot of documents in your collection: think about creating an index on historyFolders.name

gmeiner.m
  • 747
  • 5
  • 19
  • Hey. Thanks for the answer. The point is that if you write like this: `template.findOne(query, History.class);`, your `return` wait History, not Folder. I want to take Folder-model from this document – Mykola Nov 17 '20 at 09:48
  • just updated the answer. Sorry i first did not see that you want the Folder and not the History model. – gmeiner.m Nov 17 '20 at 09:49
  • Yes, I understand you. But my idea is to get the "Item" itself, not the "History" with this item. – Mykola Nov 17 '20 at 11:54
  • yes as you see in the updated answer: this is only possbile with client side filtering, filtering using projection and then unwinding client-side or with a aggregation pipepline. You can use all options with spring-data & mongodb. – gmeiner.m Nov 17 '20 at 13:33