【发布时间】: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 进行了索引,该字段是上面字符串中给出的格式的字符串。
我已经尝试了几个小时,但找不到进行此查询的方法。
【问题讨论】:
-
貌似,lte日期由于某种原因无法识别,尝试直接用BsonDocument配置:
new BsonDocument { { "lte", DateTime.Parse("2021-10-11T19:11:16.1928297Z") } }和DateTime -
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") } } } } } };
-
我不太擅长 C#,但我知道这个针对 C# Atlas Search 构建者的 repo:github.com/mongodb-labs/mongo-csharp-search
-
我现在不用图集搜索也没有办法查,这里试试mongodb.com/community/forums
标签: c# mongodb asp.net-core mongodb-atlas-search