【问题标题】:CosmosDB mongodb - $all combined with $elemMatch query on nested collection of objects not working as expectedCosmosDB mongodb - $all 结合 $elemMatch 查询嵌套的对象集合未按预期工作
【发布时间】:2019-03-28 18:35:43
【问题描述】:

我正在使用 CosmosDB(带有 mongodb-api)。

对于这个例子,我使用的是下一个文档:

/* 1 */
{
    "_id" : "abspi4lxwfbfjahdvjm3clnnl4",
    "categories" : [ 
        {
            "_name" : "category-1",
            "date" : NumberLong(1540373282070),
            "string" : "20",
            "number" : 20
        }, 
        {
            "_name" : "category-2",
            "string" : "1",
            "number" : 1
        }
    ]
}

/* 2 */
{
    "_id" : "ggi36vpvprdbrdnji5otypbh3e",
    "categories" : [ 
        {
            "_name" : "category-2",
            "date" : NumberLong(1540373282071),
            "string" : "1",
            "number" : 1
        }
    }
}

/* 3 */
{
    "_id" : "kdh3jdyenapq54clufgajmgfy8",
    "categories" : [ 
        {
            "_name" : "category-1",
            "date" : NumberLong(1540373282070),
            "string" : "20",
            "number" : 20
        }, 
        {
            "_name" : "category-2",
            "string" : "1",
            "number" : 1
        },
        {
            "_name" : "category-3",
            "string" : "29",
            "number" : 29
        }
    ]
}

我需要检索文档 1 和 3,下一个查询匹配在 mongoDB 中完美运行。

db.getCollection('test_collection')
    .find({"categories":{
        "$all":[
            {"$elemMatch":{"number":20}},
            {"$elemMatch":{"number":1}}
        ]
     }
})

在 CosmosDB 中返回 0 条记录。似乎不支持 cosmosDB 中超过 1 个 elemMatch。 知道是否可以使用 mongoDB-api 在 CosmosDB 中进行查询?

【问题讨论】:

    标签: azure azure-cosmosdb azure-cosmosdb-mongoapi


    【解决方案1】:

    对于 2018 年 10 月底创建的新数据库,此问题已得到解决。 我们做了很多测试以测试兼容性并且工作正常。

    这次我们很幸运,一直在考虑通过 SQL 查询将所有系统更改为 documentDB。

    我希望这篇文章能帮助其他可能处于相同情况的人。

    【讨论】:

      【解决方案2】:

      在 CosmosDB 中返回 0 条记录。似乎超过 1 个 elemMatch 在 不支持 cosmosDB。

      是的,根据我的测试,Cosmos Mongo API 不支持 $and 运算符和超过 1 个 elemMatch 条件的组合。据我所知,CosmosDB 仅支持 MongoDB API 的一个子集,并将请求转换为 CosmosDB SQL 等效项。 CosmosDB 有一些不同的行为和结果。但是 CosmosDB 有责任改进他们对 MongoDB 的仿真。

      当然,您可以添加反馈 here 以提交您的要求,或者如果您想要完整的 MongoDB 功能支持,请考虑在 Azure 上使用 MongoDB Atlas

      【讨论】:

      • 感谢@JayGong 的回复,我一天前与 CosmosDB 团队交谈过,他们说这个月已经解决了这个问题,但仅适用于新数据库。我的团队今天一直在测试,很多组合,似乎新功能运行良好。
      猜你喜欢
      • 1970-01-01
      • 2012-11-18
      • 2020-03-15
      • 1970-01-01
      • 2021-03-10
      • 1970-01-01
      • 2017-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多