【问题标题】:Aggregate Pipeline Atlas Search Stage using C#使用 C# 聚合管道图集搜索阶段
【发布时间】:2021-11-30 14:40:18
【问题描述】:

我需要使用 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 进行了索引,该字段是上面字符串中给出的格式的字符串。

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

【问题讨论】:

标签: c# mongodb asp.net-core mongodb-atlas-search


【解决方案1】:

看起来,由于某种原因无法识别 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") }
                    }
                }
            }
        }
    };

【讨论】:

    猜你喜欢
    • 2019-06-03
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多