【问题标题】:Find elements in a collection where an element has a field which is a list and a given value must belong to the list在集合中查找元素,其中元素具有列表字段,并且给定值必须属于列表
【发布时间】:2014-02-11 05:41:58
【问题描述】:

我想查询我的集合中的对象例如给定的值必须属于stringArray中的值

stringArray 是包含字符串列表的每个对象的字段名称

我在 mongodb 中的集合的结构是

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

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

}

我的查询是:

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

到目前为止,它还没有奏效。

有什么想法吗?

【问题讨论】:

    标签: spring mongodb criteria mongotemplate


    【解决方案1】:

    认为您刚刚翻转了那里的订单。请尝试:

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

    而不是上面的

    【讨论】:

    • 非常感谢!它有效!但是我对语法有点疑惑,我以为语法是:where(元素)。在(数组)中,而在相对性中,它是(数组)在(元素)中的位置
    • 确实,语法有点违反直觉,但它是关键,然后是可能值的列表。人们会期望在篮子中在中找到苹果,反之亦然
    猜你喜欢
    • 2020-04-12
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 2017-09-16
    • 1970-01-01
    • 2012-01-23
    相关资源
    最近更新 更多