O
O
Oleg Carp2020-11-08 18:05:41
Java
Oleg Carp, 2020-11-08 18:05:41

Java+Spring+Mongo How to pull an object from a collection by the value of an array element?

In the collection, an object containing the "ask" field as an array of options and "answer" as a string
A request comes from the client - "a certain word" and if there is such a value in the object's array, the object itself is returned.
If instead of ask just a String I got the object easily with this

@Repository
public interface PostRepository extends MongoRepository<Answer, String> {
    public Answer findAnswerByAsk(String ask);
}

I can’t figure out how to make the object return, in which direction to dig?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Carp, 2020-11-09
@Rebel-Cat

This hat was decided very simply

@Repository
public interface PostRepository extends MongoRepository<Answer, String> {

    @Query("{ 'ask': ?0}")
    public Answer findAnswerByAsk(String ask);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question