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.amo开发者_Go百科unt > 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?
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.
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 }
精彩评论