c# - 使用 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") }
}
}
}
}
};
推荐阅读
- multithreading - 如何在matlab中并行运行定时器功能多次
- mongodb - 在单个查询中更新文档并插入子文档
- java - maven项目的编码问题
- collections - Shopify 主题开发 - 如何用逗号分隔和货币显示产品价格
- c++11 - 为什么继承会导致共享内存分段错误?
- laravel - 按类别获取产品的相关产品
- java - 使用 JDT 在独立 Java 应用程序中提取 IcompilationUnits
- python-3.x - 在 Mac OS 10.14.5 上安装 pymc
- javascript - 在确认页面中,如果我单击浏览器重新加载,商品将被添加到购物车中
- c - 循环遍历数组并将某些值分配给另一个数组