命令聚合失败:来自 mongot :: 的远程错误由 :: \"origin\" 引起,必须是日期、数字或 geoPoint(来自 \"compound.should[1].near\")

Command aggregate failed: Remote error from mongot :: caused by :: \"origin\" must be a date, number, or geoPoint (from \"compound.should[1].near\")

我正在使用 MongoDB Atlas 并使用 C# 字典对象创建搜索管道,然后将其序列化为 json,然后将 json 字符串解析为 BsonDocument。

在 $search 阶段,我在 compound should 中使用 near object 进行排序。

As you can see I'm creating an object for near in which my origin is DateTime.UtcNow

Here is my function which converts Dictionary objects to BsonDocument

This is my BsonDocument after converting Dictionary objects to Json and then converting to Bson

Here I'm using it in agreegation pipeline

主要问题是 mongodb 不接受 C# DateTime 作为原始日期。

请指导我如何从我的 C# 代码中给出正确的 MongoDb 日期。

欢迎来到 Stack Overflow。也许您需要在转换整个对象之前专门使用 BsonDateTime 转换近原点参数,如下所示:

new BsonDateTime(origin)

以后,如果您尽可能粘贴代码而不是图片,我们会更容易提供帮助。