- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
目前,我在为 MongoDb 开发 Csharp Driver LinQ,但在实现类似于在 MongoDb 上调用存储函数的方法时遇到了问题。其实我知道MongoDB没有存储过程机制。我需要有人提出建议或解决方案来解决这个问题。重要的是数据将以某种方式在 mongodb 中完成,而不是在内存中完成。例如,我想返回一个列表,其中包含通过自定义方法实现的过滤条件。此方法基于字段依赖性进行计算。
一个例子在内存中完成。
var list = collection.AsQueryable<Rule>().ToList();
var result = list.Where(x => x.Active && CalcMethod(x.Rule)> 5);
这里是自定义方法。
public static int CalcMethod(Rule rule)
{
if(rule.Active)
// bypass check null
return rule.Weight.Unit * rule.Weight.Value;
else
// return something here
}
CalcMethod 方法类似于 SQL Server 中的函数。无论我们是否可以使用 MongoDb 或其他工具来做到这一点,都希望我们可以注入(inject)一种方法来计算数据和过滤,而无需在内存中完成。我们将不胜感激。
最佳答案
桑纳,我认为您可以为此使用聚合(或 MapReduce 来处理复杂的事情)。即:
async void Main()
{
var client = new MongoClient("mongodb://localhost");
var db = client.GetDatabase("TestSPLike");
var col = db.GetCollection<Rule>("rules");
await client.DropDatabaseAsync("TestSPLike"); // recreate if exists
await InsertSampleData(col); // save some sample data
var data = await col.Find( new BsonDocument() ).ToListAsync();
//data.Dump("All - initial");
/*
db.rules.aggregate(
[
{ $match:
{
"Active":true
}
},
{ $project:
{
Name: 1,
Active: 1,
Weight:1,
Produce: { $multiply: [ "$Weight.Unit", "$Weight.Value" ] }
}
},
{ $match:
{
Produce: {"$gt":5}
}
}
]
)
*/
var aggregate = col.Aggregate()
.Match(new BsonDocument{ {"Active", true} })
.Project( new BsonDocument {
{"Name", 1},
{"Active", 1},
{"Weight",1},
{"Produce",
new BsonDocument{
{ "$multiply", new BsonArray{"$Weight.Unit", "$Weight.Value"} }
}}
} )
.Match( new BsonDocument {
{ "Produce",
new BsonDocument{ {"$gt",5} }
}
})
.Project( new BsonDocument {
{"Name", 1},
{"Active", 1},
{"Weight",1}
} );
var result = await aggregate.ToListAsync();
//result.Dump();
}
private async Task InsertSampleData(IMongoCollection<Rule> col)
{
var data = new List<Rule>() {
new Rule { Name="Rule1", Active = true, Weight = new Weight { Unit=1, Value=10} },
new Rule { Name="Rule2", Active = false, Weight = new Weight { Unit=2, Value=3} },
new Rule { Name="Rule3", Active = true, Weight = new Weight { Unit=1, Value=4} },
new Rule { Name="Rule4", Active = true, Weight = new Weight { Unit=2, Value=2} },
new Rule { Name="Rule5", Active = false, Weight = new Weight { Unit=1, Value=5} },
new Rule { Name="Rule6", Active = true, Weight = new Weight { Unit=2, Value=4} },
};
await col.InsertManyAsync( data,new InsertManyOptions{ IsOrdered=true});
}
public class Weight
{
public int Unit { get; set; }
public int Value { get; set; }
}
public class Rule
{
public ObjectId _id { get; set; }
public string Name { get; set; }
public bool Active { get; set; }
public Weight Weight { get; set; }
}
关于c# - 使用 Mongodb-CSharp Driver 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31676453/
我正在尝试获取此亚马逊页面中列出的每台笔记本电脑的图像 URL ( https://www.amazon.com/s?rh=n%3A565108%2Cp_72%3A4-&pf_rd_i=565108&
我正在设置 Atlassian Confluence,在选择数据库时,我在选择“使用外部 Mysql 数据库”时卡住了我看过一些教程,但对我来说,它并没有按照应有的方式工作。我使用 ubuntu 12
我是 Neo4J 的新手,正在尝试通过 java 连接到 Neo4J 服务器。 我的一个独立项目的pom入口如下: org.neo4j neo4j-o
所有这些有什么区别和用途? spark.local.ip spark.driver.host spark.driver.bind地址 spark.driver.hostname 如何将机器修复为 Sp
我在旧的 Inspiron 6400 计算机(GeForce 7300 笔记本电脑版)上安装了 Lubuntu 19.04,通过网络草稿编辑器教我儿子 Scratch。每次我通过 Firefox 打开
我使用 qt 开发了一个 c++ 库。在本文中,我使用 QSqlDatabase 从 SQLite 数据库中查询信息。注意:我的库在 qt 桌面应用程序中运行良好(我在 Linux 上开发)。 现在我
存在类似的问题,但没有帮助。 在 Apache 2.4 上安装 php5-fpm 通过 SSL 连接到远程 MySql 数据库。 可以通过命令行连接MySQL mysql -u myname -p'p
使用以下配置 (doctrine.yaml) 在 Symfony 4 中使用 Doctrine DBAL: dbal: # configure these for your database
使用以下配置 (doctrine.yaml) 在 Symfony 4 中使用 Doctrine DBAL: dbal: # configure these for your database
我有一个用 Java 编写的 Selenium Web 驱动程序测试,目标是 Liferay 站点。 // Login driver.get(baseUrl + "/"); driver.findEl
在driver.findElements()中,我们获得了另一个用于查找size()的函数,该函数在driver.findElement()中不可用。 这是唯一的区别吗? 最佳答案 driver.fi
这个问题已经有答案了: java.lang.IllegalStateException: The driver executable does not exist: while trying to e
简短描述:我有一个通过 SignTool 验证的签名驱动程序,但 Windows 拒绝加载它并出现错误:CodeIntegrity 3004 - 在系统上找不到文件哈希。我该如何解决这个问题? 详细说
我正在设置一些 Geb 测试,但出现“geb.driver.DriverCreationException:无法从回调创建驱动程序”错误。 Geb 将尝试启动测试浏览器窗口,但一旦启动,我的所有测试都
我想通过应用对象存储库概念在 Chrome 驱动程序中打开 url。下面给出的是我的 selenium 程序,其中包含两个文件,一个是 testng 文件,另一个是 config.property 文
我在 Ubuntu Linux、Spring Tools 2.7.1、Spring Roo 1.1.5 上运行 Eclipse Indigo。我正在阅读 Getting Started with Ro
打开 Url 的首选方法是什么(它们之间是否存在任何差异): driver.Url = "http://example.com"; 或 driver.Navigate().GoToUrl("http:
我使用 python 脚本传递给 cassandra 批处理查询,如下所示: query = 'BEGIN BATCH ' + 'insert into ... ; insert into ... ;
我在 Protractor 中执行脚本时出现以下错误。 System info: host: '8888', ip: '88888', os.name: 'Mac OS X', os.arch: 'x
我收到错误 KeyError:'driver'。 我想使用scrapy-selenium 创建一个网络爬虫。我的代码如下所示: class TestSpider(Spider): name="test
我是一名优秀的程序员,十分优秀!