10

I would like to query the objects in my collection such as a given value must belong to the values in the stringArray

stringArray is the name of the field for each Obejct that contains a list of Strings

the strucutre of my collection in mongodb is

Object1
{
  field1
  field2
  stringArray[2] 
        0     String0
        1     String1
}

Object2
{
  field1
  field2
  stringArray[3] 
        0     String0
        1     String1
        2     String2
}

}

My query is:

     Query query = new Query();
     query.addCriteria(
            Criteria.where(theValueIamlookingFor).in("stringArray")                
     );               
    return mongoTemplate.find(query, myObject.class);

So far, it hasn't worked.

Any ideas ?

Ori Dar
  • 18,687
  • 5
  • 58
  • 72
Makoto
  • 765
  • 2
  • 17
  • 45

1 Answers1

16

Think you have just flipped there order. Please try:

Criteria.where("stringArray").in(theValueIamlookingFor)

instead of the above

Ori Dar
  • 18,687
  • 5
  • 58
  • 72
  • Thanks a lot !It works ! But I am a bit puzzled by the syntax, I thought that the syntax was: where (the element). in (the array) while in relaity it is where(the array) in (the element) – Makoto Jan 21 '14 at 17:05
  • Indeed, the syntax is a bit counter-intuitive, but it's the key first, followed by a list of possible values. One would have expected to find the apples **in** the basket and not vice versa – Ori Dar Jan 21 '14 at 17:13