【问题标题】:Using both $in and $elemMatch using the C# Driver使用 C# 驱动程序同时使用 $in 和 $elemMatch
【发布时间】:2023-03-23 13:08:01
【问题描述】:

我很难将工作中的MongoDB 查询转换为 C# 驱动程序的无类型等效项。 查询:

{
    "Field" : { "$elemMatch" : { "$in" : ["Hamster"]}}
}

我有什么:

Query.ElemMatch("Field", Query.In("", new BsonArray(new[] { "Hamster" })));

生成:

{
    "Field" : { "$elemMatch" : { "" : { "$in" : ["Hamster"] }}}
}

这非常接近,但我不知道如何从 $in 查询中删除名称。

【问题讨论】:

    标签: c# .net mongodb mongodb-.net-driver


    【解决方案1】:
    Query.ElemMatch("Field", new QueryDocument("$in", new BsonArray(new[] { "Hamster" })));
    

    【讨论】:

      【解决方案2】:

      看起来应该是可能的,但是辅助方法的结构方式我看不到直接构建该查询的简单方法。

      我可以重新创建所需查询的唯一方法是执行以下变体:

      var queryDocument = QueryDocument.Parse("{\"$in\" : [\"Hamster\"]}");
      var nestedQueryDocument = Query.ElemMatch("Field", new QueryDocument(queryDocument));
      

      创建以下输出

      {
          "Field" : { "$elemMatch" : { "$in" : ["Hamster"]}}
      }
      

      虽然不是最优雅的解决方案。

      【讨论】:

        猜你喜欢
        • 2020-04-12
        • 2017-03-01
        • 2014-01-30
        • 2016-10-09
        • 1970-01-01
        • 2022-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多