q0987
q0987

Reputation: 35984

How to check when find return nothing in 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?

Upvotes: 2

Views: 1524

Answers (1)

Aurélien B
Aurélien B

Reputation: 4640

> 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.

Upvotes: 2

Related Questions