K
K
kr1337ol2021-01-11 15:39:08
Java
kr1337ol, 2021-01-11 15:39:08

How does mongoDB array lookup via Spring Data work?

There is such a couple of thousand objects like this:

{
      "_id" : ObjectId("5ffbfca73fb224209c6d27be"),
      "activeStates" : ["logicConnectionLost", ...],
       ...,
      "_class" : "Entity"
    }

I need to count the number of objects whose activeStates array contains at least one element from the input array?
I am using the
countByActiveStatesIn(Set states);
Does Mongo compare each against each until the end of all arrays, or until it finds the first match?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2021-01-19
@Asapin

The answer depends on what kind of request the Spring Data will generate for the countByActiveStatesIn(Set states);.
According to table #16 in section 15.3 of the Spring Data MongoDB documentation , a query should be generated

db.collectionName.count({ activeStates: { $in: ["logicConnectionLost"] }})

which will count all documents whose field activeStatescontains at least one element from the passed list.
Which request was generated can be checked by setting the logging level to DEBUG:
logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG

#Если используете реактивный MongoTemplate:
logging.level.org.springframework.data.mongodb.core.ReactiveMongoTemplate=DEBUG

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question