MongoDB 高级查询
假设我有一个具有两个属性的集合:amount 和 amountreceived 都是小数。
我想编写一个查询,该查询将从集合中返回满足金额大于收到金额条件的项目。
目前我有一个如下所示的 javascript 函数:
f = function() { return this.amount > this.amountreceived;}
我还有一个如下所示的集合集:
item1 : amount = 50, amountreceived = 0;
item2 : amount = 50, amountreceived = 25;
如果我运行 db.MyCollection.find(f) ,则返回的唯一结果是 item1。
关于为什么该查询不会返回两个结果有什么想法吗?
Let's say I have a Collection with two properties: amount and amountreceived that are decimals.
I want to write a query that will return items from the collection that meet the criteria of the amount being greater than the amount received.
Currently I have a javascript function that looks like this:
f = function() { return this.amount > this.amountreceived;}
I also have a collection set that looks like this:
item1 : amount = 50, amountreceived = 0;
item2 : amount = 50, amountreceived = 25;
If I run db.MyCollection.find(f)
the only result returned is item1.
Any ideas on why that query will not return both results?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您使用的是 C# 驱动程序?由于 BSON 没有十进制数据类型,C# 驱动程序必须以某种方式表示 .NET 十进制值。默认表示形式是字符串,这有利于不丢失任何精度,但不利于执行查询。
您还可以要求 C# 驱动程序将 .NET 十进制值存储为 BSON 双精度数,这会导致一些精度损失并可能溢出,但有利于查询。
I'm assuming you are using the C# driver? Because BSON does not have a decimal data type the C# driver has to represent the .NET decimal value in some way. The default representation is as a string, which is good for not losing any precision but bad for doing queries.
You can also ask the C# driver to store .NET decimal values as BSON doubles, which would involve some loss of precision and could overflow, but would be good for queries.
您确定您这样做正确吗?对于我下面的例子,你有一个反例吗?
这是我的示例测试脚本,它可以工作。 (
db.foo
为空)Are you sure that you are doing this correctly? Do you have a counter-example for my example below?
Here's my sample test script where this works. (
db.foo
is empty)