【问题标题】:jongo find subdocuments with array of valuesjongo 查找具有值数组的子文档
【发布时间】:2014-06-18 13:49:17
【问题描述】:

我的数据如下所示:

"_id" : ObjectId("53a173630364206735975b35"),
"username" : "TestUserID",
"resources" : [
    {
        "id" : "FirstTestResourceId",
        "tags" : [
            "TestResourceTag1",
            "TestResourceTag2"
        ],
    }
    {
        "id" : "SecondTestResourceId",
        "tags" : [
            "TestResourceTag1",
            "TestResourceTag2"
        ],
    }
]

我想做的是检索用户的所有资源文档,其中任何标签与字符串数组的任何成员匹配。

当我这样做时:

db.collection.find({username: 'TestUserID', 'resources.tags': { $in: ['TestResourceTag1']}})

它似乎工作正常(因为它用正确的资源子文档带回了正确的文档,但是当我在我的 java 类中尝试它时,它带回了 nada。

Iterable<Resource> resources = userCollection.find("{username: #, 'resources.tags': { $in: #}}", userID, tags).as(Resource.class);

其中 userID = String 和 tags = String[]

我想我在查询中做了一些模糊的事情,但我似乎无法在任何地方找到答案。

如果能提供有关此问题的任何指导,我将不胜感激。

【问题讨论】:

    标签: mongodb jongo


    【解决方案1】:

    为您的tags 变量使用List&lt;String&gt; 而不是String 数组,所以不是

    String[] tags = new String[]{"TestResourceTag1"};
    

    尝试使用

    List<String> tags = Arrays.asList("TestResourceTag1");
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-02
      • 2012-08-12
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      • 1970-01-01
      • 2014-12-11
      相关资源
      最近更新 更多