[英]MongoDB C# 2.0 Driver Query Where Field A is Less Than Field B
I am using the C# 2.0 MongoDB Driver. 我正在使用C#2.0 MongoDB驱动程序。
I am trying to find all documents where field A is less than field B. 我正在尝试查找字段A小于字段B的所有文档。
I tried this: 我尝试了这个:
var filter = Builders<Stock>.Filter.Where(s => s.LastPrice < s.MosPrice);
var result = await _collection.Find(filter).ToListAsync();
return result;
But I received the following error: 但是我收到以下错误:
System.ArgumentException Unsupported filter: (Serialization(LastPrice) < Serialization(MosPrice)).
System.ArgumentException不支持的过滤器:(Serialization(LastPrice)<Serialization(MosPrice))。
How can I perform this query using the new C# MongoDB Driver? 如何使用新的C#MongoDB驱动程序执行此查询?
To solve this type of queries I use BsonJavaScript
: 为了解决这类查询,我使用了
BsonJavaScript
:
With old driver : 与老司机 :
var filter = Query.Where(new BsonJavaScript("this.LastPrice < this.MosPrice"));
With new driver : 使用新驱动程序 :
var filter = new BsonDocument(new BsonDocument("$where", new BsonJavaScript("this.LastPrice < this.MosPrice")));
Problem occurs becouse in where condition you try to filter by not constant value. 出现问题是因为您尝试使用非恒定值进行过滤的条件。 Mongo db driver not support this case.
Mongo数据库驱动程序不支持这种情况。
Seems that you simply cannot: 似乎您根本无法:
"The issue with your query is that MongoDB does not allowing comparing two fields to each other. You can only compare fields to a constant. Because of this restriction, we cannot convert the query. It is simply not valid MongoDB syntax. “查询的问题在于MongoDB不允许将两个字段相互比较。您只能将字段与常量进行比较。由于这一限制,我们无法转换查询。这完全不是有效的MongoDB语法。
I'm going to close this ticket as Works as Designed." 我将按设计原样关闭这张票。”
https://jira.mongodb.org/browse/CSHARP-1592 https://jira.mongodb.org/browse/CSHARP-1592
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.