使用 C# 聚合管道图集搜索阶段

Aggregate Pipeline Atlas Search Stage using C#

我需要使用 C# 进行此查询:

{
$search:{
{
  range:{
    path:"canvasAi.publication_date",
    gte:ISODate("2021-04-01T00:00:00Z"),
    lte:ISODate("2021-10-11T19:11:16.1928297Z")
  }
}
}
}

到目前为止我有这个:

var query = new JObject(
                    new JProperty("$search", new JObject(
                            new JProperty("range", new JObject(
                                    new JProperty("path", "canvasAi.publication_date"),
                                    new JProperty("gte", "ISODate('2021-04-01T00:00:00Z')"),
                                    new JProperty("lte", "ISODate('2021-10-11T19:11:16.1928297Z')")
                                ))
                        ))
                );

            var new_query = JsonConvert.SerializeObject(query);

var pipeline = new BsonDocument[]
            {
                BsonDocument.Parse(new_query)
            };
var result = collection.Aggregate<BsonDocument>(pipeline);

我一直收到来自 mongo 的错误,即 lte 必须是数字,日期。

我使用 Atlas Search 为 canvasAi.publication_date 建立了索引,该字段是一个字符串,其格式为上面的字符串。

我已经尝试了几个小时,但找不到进行此查询的方法。

看起来,lte 日期由于某种原因无法识别。尝试使用 BsonDocument 表单:

    var query = new BsonDocument
    {
        { 
            "$search",
            new BsonDocument
            {
                {
                    "range",
                    new BsonDocument
                    {
                        { "path", "canvasAi.publication_date" },
                        { "gte", DateTime.Parse("2021-04-01T00:00:00Z") },
                        { "lte", DateTime.Parse("2021-10-11T19:11:16.1928297Z") }
                    }
                }
            }
        }
    };