Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoRepository Find List in Array

I've got a document that looks like this

@Document
public @Data class Note {

    @Id
    private String noteId;
    private String owner;
    @TextIndexed
    private String name;
    @TextIndexed
    private String text;
    private List<String> tags;
    private LocalDate date;
}

Also I'm using spring data mongodb to operate on mongodb datastore. I've created interface

public interface NoteRepository extends MongoRepository<Note, String> {
    List<Note> findByTags(List<String> tags);
}

My stored object looks like this

[
    {
        "noteId": "594e4adc3bc5152218f933b4",
        "owner": "system",
        "name": "simple note",
        "text": "My text",
        "tags": [
            "tag1",
            "tag2",
            "tag3"
        ],
        "date": [
            1992,
            12,
            15
        ]
    }
]

But unless I don't provide the list of tags like tag1, tag2, tag3 to findByTags method it won't return any result. For example tag1, tag2 returns nothing etc. etc.

How should I conduct the search over such tags? Use some TextCriteria?

like image 988
lapots Avatar asked Jun 24 '17 12:06

lapots


People also ask

How to search in array in MongoDB?

To search the array of object in MongoDB, you can use $elemMatch operator. This operator allows us to search for more than one component from an array object.

Why does spring boot with MongoDB Findall () return empty array?

The issue arises because the collection name is not explicitly given in your model class, so spring-data derives the collection name from the class name ( Courses ) into camel case ( courses ). Since your actual collection is called Courses , no results are found.

What is MongoRepository?

MongoRepository is an interface provided by Spring Data in the package org. springframework. data. mongodb.

What is Spring Data MongoDB?

Spring Data for MongoDB is part of the umbrella Spring Data project which aims to provide a familiar and consistent Spring-based programming model for new datastores while retaining store-specific features and capabilities.


1 Answers

It depends on what you want to provide.

If you just want a single value then MongoDB does not care if the data is in an array and will simply look for the match in all entries

public interface NoteRepository extends MongoRepository<Note, String> {
    List<Note> findByTags(String tags);
}

If you want a list of variable size for comparison of "any" that may match, then there is a keyword used by spring-mongo that effects the $in operation:

public interface NoteRepository extends MongoRepository<Note, String> {
    List<Note> findByTagsIn(List<String> tags);
}

So the first is for where you just want "tag1" to match the data. And the second would match a List like "tag1", "tag2", or anything as long as 'at least one' was actually present in the array being searched.

like image 131
Neil Lunn Avatar answered Sep 24 '22 08:09

Neil Lunn