MongoDB 高级查询

发布于 2024-10-22 01:15:56 字数 450 浏览 3 评论 0原文

假设我有一个具有两个属性的集合: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

焚却相思 2024-10-29 01:15:56

我假设您使用的是 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.

Oo萌小芽oO 2024-10-29 01:15:56

您确定您这样做正确吗?对于我下面的例子,你有一个反例吗?

这是我的示例测试脚本,它可以工作。 (db.foo 为空)

MongoDB shell version: 1.6.5
connecting to: test
> db.foo.insert( {_id : 1, amount : 50, amtreceived : 100 } )
> db.foo.insert( {_id : 2, amount : 50, amtreceived : 25 } )
> db.foo.insert( {_id : 3, amount : 50, amtreceived : 0 } )
> db.foo.find()
{ "_id" : 1, "amount" : 50, "amtreceived" : 100 }
{ "_id" : 2, "amount" : 50, "amtreceived" : 25 }
{ "_id" : 3, "amount" : 50, "amtreceived" : 0 }
> f = function() { return this.amount > this.amtreceived; }
function () {
    return this.amount > this.amtreceived;
}
> db.foo.find(f) // expect 2 & 3
{ "_id" : 2, "amount" : 50, "amtreceived" : 25 }
{ "_id" : 3, "amount" : 50, "amtreceived" : 0 }

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)

MongoDB shell version: 1.6.5
connecting to: test
> db.foo.insert( {_id : 1, amount : 50, amtreceived : 100 } )
> db.foo.insert( {_id : 2, amount : 50, amtreceived : 25 } )
> db.foo.insert( {_id : 3, amount : 50, amtreceived : 0 } )
> db.foo.find()
{ "_id" : 1, "amount" : 50, "amtreceived" : 100 }
{ "_id" : 2, "amount" : 50, "amtreceived" : 25 }
{ "_id" : 3, "amount" : 50, "amtreceived" : 0 }
> f = function() { return this.amount > this.amtreceived; }
function () {
    return this.amount > this.amtreceived;
}
> db.foo.find(f) // expect 2 & 3
{ "_id" : 2, "amount" : 50, "amtreceived" : 25 }
{ "_id" : 3, "amount" : 50, "amtreceived" : 0 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文