How to check when find return nothing in MongoDB - mongodb

> db.c.remove()
> db.c.insert( { x : 10} )
> a1 = db.c.findOne( {x : 100} )
null
> a1 == null
true
> a2 = db.c.find( {x : 100} )
>
Q1> How do I check if a2 is assigned with nothing inside the MongoDB interactive shell?
Q2> How to I check the same thing in Python 3?

> a2 = db.c.find( {x : 100} )
> a2.hasNext()
false
Because you a2 variable is indeed an MongoCursor.
And a lot of language drivers have this kind of method.
Hint: you can use variable.help() to have some function on mongo client command line.
Edit about Python: I don't know exactly how it works but regarding the documentation Python driver return an iterable element. So it's probably the same way than other languages.

Related

Is there a continue in minizinc or what is the alternative and how to convert if else in predicate?

So like in python and other languages is there a continue statement in minizinc? below I have my minizinc if else statement how can I convert them into a predicate way ?
if sum_neighbors(x,y) = 3
then grid[x,y]=1
elseif sum_neighbors(x,y)=2
then grid[x,y]=grid[x,y]
else grid[x,y]=0
endif
How can I convert this into predicate form ? I tried the following but it does not work.
(sum_neighbors(x,y) = 3 )
-> grid[x,y]=1
/\ ( sum_neighbors(x,y)=2 )
-> grid[x,y]=grid[x,y]
/\ (sum_neighbors(x,y)!=3 /\sum_neighbors(x,y)!=2) -> grid[x,y]=0

How to check basic integer order relations in sympy?

I have a (possibly relatively large) set of assumptions about multiple integers like {x > -1, x < 5, x != 2, y > 0, x-2 < y} and I would like to check whether certain other propositions like {x > -5, x == 3, ...} either true, false or could be both.
The docs say that explicit relationships like Q.is_true(x < 3) are not supported, so I tried using .positive property, but without any luck, e.g.
# x > -1 => x > -3 - ?
x = sympy.Symbol('x')
with sympy.assuming(sympy.Q.positive(x+1), sympy.Q.integer(x)):
print(sympy.ask(sympy.Q.positive(x+3)))
produces
None
Which means that the checker gave up on checking that.
Refine also does not seem to help much (probably uses assumptions anyway)
sympy.refine(x > 0, sympy.Q.is_true(x > -1))
If there's a different library that can check that, that also works!
I have found that python bindings for the z3 solver best fitted for my problem. One can just download the binary release from the github page and add included folder into $PYTHONPATH , e.g.
LD_LIBRARY_PATH=${Z3FOLDER}/bin PYTHONPATH=${Z3FOLDER}/bin/python python
then these relations could be checked as
from z3 import *
x = Int('x')
s = Solver()
s.add(x > 10)
s.add(x > 12)
print(s) // [x > 10, x > 12]
print(s.check()) // sat
print(s.model()) // [x = 13]

how to write ( A after max time B ) in CEP( Fusion ) Drools

What is the best way to write a condition in drools CEP to infer ( A after max time B )
Example :
a : new A();
b : new B( this after [1m] )
The above example is not my need.
I need this :
a : new A();
b : new B( this after a , b.timestamp - a.timestamp <= 60000)
So i reformulate the question. Is another way to obtain the same result with less instructions ?
Thanks
Edit after clarification of Q
$a: A()
$b: B( this after[ 0s, 60s ] $a )
This fires if B comes after A but not later than 60 seconds.

Mongo db shell variable printing only once

> var x = db.sampleDB.find();
> x
output:
{ "_id" : ObjectId("55d02ed690ddbaafbfe20898"), "name" : "aditya", "city" : "meerut" }
But if I print this variable again, it does not printing anything.
and I am not able to print x.name?
using ubuntu 14.04(db version v2.6.10)
The result returned from .find() is a "cursor" and not a value. So when you do something like:
> var x = db.sampleDB.find();
> x
Then all you are doing is iterating the cursor just as if you did:
> db.sampleDB.find();
If you wanted to "keep" the content, then call .toArray()
> var x = db.sampleDB.find().toArray();
> x
Or if the result is singular, then just call .findOne()
> var x = db.sampleDB.findOne();
> x
These have now all been "converted" from a cursor, which only retrieves results once, in to variable that already has the fetched results.

MongoDB's NumberLong only 54 bit signed?

Is a MongoDB's NumberLong really 64 bit signed integer really 64 bit?
MongoDB's NumberLong is said to be a 64 bit signed integer, which should mean we can play with -2^63 <= x <= 2^63-1, where x is a NumberLong.
However, adding 1 or subtracting 1 from a NumberLong(x) does not return the expected value for x <= -2^54 or x >= 2^54, but correct values are returned for -2^53 <= x <= 2^53.
The reliable NumberLong numbers therefor seem to be 54 bit signed integers.
Why is this?
Am I doing someting wrong?
Sample from the mongo shell:
> NumberLong( Math.pow(2,54) )
NumberLong("18014398509481984") // Expected
> NumberLong( Math.pow(2,54)-1 )
NumberLong("18014398509481984") // **NOT** Expected
> NumberLong( -Math.pow(2,54) )
NumberLong("-18014398509481984") // Expected
> NumberLong( -Math.pow(2,54)+1 )
NumberLong("-18014398509481984") // **NOT** Expected
> NumberLong( Math.pow(2,53) )
NumberLong("9007199254740992") // Expected
> NumberLong( Math.pow(2,53)-1 )
NumberLong("9007199254740991") // Expected
> NumberLong( -Math.pow(2,53) )
NumberLong("-9007199254740992") // Expected
> NumberLong( -Math.pow(2,53)+1 )
NumberLong("-9007199254740991") // Expected
Using MongoDB 2.0.0
Wow, this is puzzling. Clearly there is some sort of rounding error happening here.
> NumberLong("18014398509481984")-NumberLong("1");
18014398509481984
> NumberLong("18014398509481984")-NumberLong("2");
18014398509481982
> NumberLong("18014398509481984")+NumberLong("1");
18014398509481984
> NumberLong("18014398509481984")+NumberLong("2");
18014398509481984
> NumberLong("18014398509481984")+NumberLong("3");
18014398509481988
This is probably something wrong with the JavaScript engine that the shell runs in, rather than MongoDB itself. Check this out, for example--$inc works fine:
> db.test.insert({twoTo54:NumberLong("18014398509481984")});
> db.test.update({},{$inc:{twoTo54:NumberLong("1")}});
> db.test.find();
{ "_id" : ObjectId("4f204847756aa806028abce1"), "twoTo54" : NumberLong("18014398509481985") }
> db.test.update({},{$inc:{twoTo54:NumberLong("1")}});
> db.test.find();
{ "_id" : ObjectId("4f204847756aa806028abce1"), "twoTo54" : NumberLong("18014398509481986") }
> db.test.update({},{$inc:{twoTo54:NumberLong("1")}});
> db.test.find();
{ "_id" : ObjectId("4f204847756aa806028abce1"), "twoTo54" : NumberLong("18014398509481987") }
You have to be careful, though. If you use just a normal literal 1, it will convert the type to a Number, which then breaks the $inc:
> db.test.update({},{$inc:{twoTo54:1}});
> db.test.find();
{ "_id" : ObjectId("4f204847756aa806028abce1"), "twoTo54" : 18014398509481988 }
> db.test.update({},{$inc:{twoTo54:1}});
> db.test.find();
{ "_id" : ObjectId("4f204847756aa806028abce1"), "twoTo54" : 18014398509481988 }
And even if you go back to $inc with NumberLong("1"), it's still broken:
> db.test.update({},{$inc:{twoTo54:NumberLong("1")}});
> db.test.find();
{ "_id" : ObjectId("4f204847756aa806028abce1"), "twoTo54" : 18014398509481988 }
Definitely good to keep in mind.
Math.pow most likely operates on doubles. The integers are coerced to floats, and at 2^54, double precision numbers lose resolution at the 1's place. The information is already lost at the time you convert back to integer types.
do you get comparable results using left shift << or repeated multiplication?