使用Mongo驱动程序查询数组中的值的CosmoDB似乎不可能

时间:2017-06-28 02:26:02

标签: java azure-cosmosdb

我们正在使用Mongo Java Client从mongoDB迁移到CosmoDB。我们在使用数组的查询行为中遇到以下差异。

我们的文档类似于以下内容

[{
    "name":"garry",
    "pets":["cats","dogs"]
},
{
    "name":"sally",
    "pets":["cats","fish"]
}]

使用mongo查询
find({"pets":"cats"})

将返回garrysally但是使用cosmoDB我们得到零结果。 有没有办法修改查询以复制相同的mongo行为?

我们还有类似以下类型的文档,我们在类型

上查询

[{ "name": "garry", "pets": [{ "type": "cat", "name": "Mittens" }, { "type": "dog", "name": "Max" }] }, { "name": "sally", "pets": [{ "type": "cat", "name": "Paul" }, { "type": "fish", "name": "Bubbles" }] } ]

当前的mongo查询看起来像是 find({"pets.type": "fish"})

1 个答案:

答案 0 :(得分:0)

我尝试使用下面的查询,它有效。

  1. 使用MongoShell。

    find({"pets": {$all: ["cats"]}})
    
  2. 对于Mongo Java驱动程序

    FindIterable<Document> queryResult = collection.find(Filters.all("pets", "cats"));