【问题标题】:Is there a way to call a user-defined function in mongo from C# code?有没有办法从 C# 代码调用 mongo 中的用户定义函数?
【发布时间】:2022-01-02 13:22:54
【问题描述】:

我在我的 Mongo 数据库中添加了以下函数:

db.system.js.save({
    _id: "testFunction",
    value: function (int) {
           
    return int;
    }
})

我在我的 C# 项目中使用以下包:

mongocsharpdriver 2.14.1 MongoDB.Driver 2.14.1

该函数在 Mongo shell 中运行没有问题。

有没有办法在 C# 代码中运行它? 我已经搜索了几个小时的解决方案,但没有运气。

请注意Eval方法/选项在上述驱动版本中不可用,所以请不要推荐。

此外,我知道 CommandDocument 类,但不知道如何创建它来调用上述函数。

提前致谢!

【问题讨论】:

    标签: javascript c# mongodb


    【解决方案1】:

    我也在寻找相同的东西,但没有找到任何相关的东西,所以我决定使用 c# 中的 aggrgate() 函数,如果你没有在 MongoDB 函数中执行复杂的查询,我也会向你建议。聚合函数还可以接受 json 格式作为 MongoDB 查询,因此您可以尝试这样的事情。

           database.GetCollection<BsonDocument>("collection").Aggregate()
              .Lookup("ForeignCollection", "LocalName", "ForeignName", "Object").Match("AnyFilter").Project(Builders<BsonDocument>.Projection.Exclude("field1").Include("field2")).ToList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 2011-02-01
      相关资源
      最近更新 更多