Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python MongoDB Query Documents

Python MongoDB Query Documents

Python Mongodb Python Mongodb

In MongoDB, the find and find_one methods are used to query data in a collection; they are similar to the SELECT statement in SQL.

The test data used in this article is as follows:

Query a single document

We can use the find_one() method to query a single document in a collection.

Query sites The first document in the collection:

Example

#!/usr/bin/python3

import

pymongo

myclient

=

pymongo

.

MongoClient

(

"

mongodb://localhost:27017/

"

)

mydb

=

myclient

[

"

runoobdb

"

]

mycol

=

mydb

[

"

sites

"

]

x

=

mycol

.

find_one

(

)

print

(

x

)

The output is:

{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'RUNOOB', 'alexa': '10000', 'url': 'https://www.runoob.com'}

Query all data in a collection

The find() method can query all data in a collection, similar to the SELECT * operation in SQL.

The following example queries sites all data in the collection:

Example

#!/usr/bin/python3

import

pymongo

myclient

=

pymongo

.

MongoClient

(

"

mongodb://localhost:27017/

"

)

mydb

=

myclient

[

"

runoobdb

"

]

mycol

=

mydb

[

"

sites

"

]

for

x

in

mycol

.

find

(

)

:

print

(

x

)

The output is:


{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'RUNOOB', 'alexa': '10000', 'url': 'https://www.runoob.com'}
{'_id': ObjectId('5b2369cac315325f3698a1cf'), 'name': 'Google', 'alexa': '1', 'url': 'https://www.google.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb6'), 'name': 'Taobao', 'alexa': '100', 'url': 'https://www.taobao.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb7'), 'name': 'QQ', 'alexa': '101', 'url': 'https://www.qq.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb8'), 'name': 'Facebook', 'alexa': '10', 'url': 'https://www.facebook.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb9'), 'name': '知乎', 'alexa': '103', 'url': 'https://www.zhihu.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbba'), 'name': 'Github', 'alexa': '109', 'url': 'https://www.github.com'}

Query data of specified fields

We can use the find() method to query data of specified fields by setting the value of the fields to return to 1.

Example

#!/usr/bin/python3

import

pymongo

myclient

=

pymongo

.

MongoClient

(

"

mongodb://localhost:27017/

"

)

mydb

=

myclient

[

"

runoobdb

"

]

mycol

=

mydb

[

"

sites

"

]

for

x

in

mycol

.

find

(

{},{

"

_id

"

:

0

,

"

name

"

:

1

,

"

alexa

"

:

1

}

)

:

print

(

x

)

The output is:


{'name': 'RUNOOB', 'alexa': '10000'}
{'name': 'Google', 'alexa': '1'}
{'name': 'Taobao', 'alexa': '100'}
{'name': 'QQ', 'alexa': '101'}
{'name': 'Facebook', 'alexa': '10'}
{'name': '知乎', 'alexa': '103'}
{'name': 'Github', 'alexa': '109'}

Except for _id, you cannot specify both 0 and 1 in one object; if you set a field to 0, all others are 1, and vice versa.

The following example returns all fields except the alexa field:

Example

#!/usr/bin/python3

import

pymongo

myclient

=

pymongo

.

MongoClient

(

"

mongodb://localhost:27017/

"

)

mydb

=

myclient

[

"

runoobdb

"

]

mycol

=

mydb

[

"

sites

"

]

for

x

in

mycol

.

find

(

{},{

"

alexa

"

:

0

}

)

:

print

(

x

)

The output is:


{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'RUNOOB', 'url': 'https://www.runoob.com'}
{'_id': ObjectId('5b2369cac315325f3698a1cf'), 'name': 'Google', 'url': 'https://www.google.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb6'), 'name': 'Taobao', 'url': 'https://www.taobao.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb7'), 'name': 'QQ', 'url': 'https://www.qq.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb8'), 'name': 'Facebook', 'url': 'https://www.facebook.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb9'), 'name': '知乎', 'url': 'https://www.zhihu.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbba'), 'name': 'Github', 'url': 'https://www.github.com'}

The following code specifying both 0 and 1 will raise an error:

Example

#!/usr/bin/python3

import

pymongo

myclient

=

pymongo

.

MongoClient

(

"

mongodb://localhost:27017/

"

)

mydb

=

myclient

[

"

runoobdb

"

]

mycol

=

mydb

[

"

sites

"

]

for

x

in

mycol

.

find

(

{},{

"

name

"

:

1

,

"

alexa

"

:

0

}

)

:

print

(

x

)

The error message is roughly as follows:

...
pymongo.errors.OperationFailure: Projection cannot have a mix of inclusion and exclusion.
...

Query by specified conditions

We can set parameters in find() to filter data.

The following example finds data where the name field is "RUNOOB":

Example

#!/usr/bin/python3

import

pymongo

myclient

=

pymongo

.

MongoClient

(

"

mongodb://localhost:27017/

"

)

mydb

=

myclient

[

"

runoobdb

"

]

mycol

=

mydb

[

"

sites

"

]

myquery

= {

"

name

"

:

"

RUNOOB

"

}

mydoc

=

mycol

.

find

(

myquery

)

for

x

in

mydoc

:

print

(

x

)

The output is:

{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'RUNOOB', 'alexa': '10000', 'url': 'https://www.runoob.com'}

Advanced queries

In query condition statements, we can also use modifiers.

The following example reads data whose first letter of the name field has an ASCII value greater than "H"; the "greater than" modifier condition is {"$gt": "H"}:

Example

#!/usr/bin/python3

import

pymongo

myclient

=

pymongo

.

MongoClient

(

"

mongodb://localhost:27017/

"

)

mydb

=

myclient

[

"

runoobdb

"

]

mycol

=

mydb

[

"

sites

"

]

myquery

= {

"

name

"

: {

"

$gt

"

:

"

H

"

} }

mydoc

=

mycol

.

find

(

myquery

)

for

x

in

mydoc

:

print

(

x

)

The output is:


{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'RUNOOB', 'alexa': '10000', 'url': 'https://www.runoob.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb6'), 'name': 'Taobao', 'alexa': '100', 'url': 'https://www.taobao.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb7'), 'name': 'QQ', 'alexa': '101', 'url': 'https://www.qq.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb9'), 'name': '知乎', 'alexa': '103', 'url': 'https://www.zhihu.com'}

Query using regular expressions

We can also use regular expressions as modifiers.

Regular expression modifiers are only used for fields that search strings.

The following example reads data whose first letter of the name field is "R"; the regular expression modifier condition is {"$regex": "^R"}:

Example

#!/usr/bin/python3

import

pymongo

myclient

=

pymongo

.

MongoClient

(

"

mongodb://localhost:27017/

"

)

mydb

=

myclient

[

"

runoobdb

"

]

mycol

=

mydb

[

"

sites

"

]

myquery

= {

"

name

"

: {

"

$regex

"

:

"

^R

"

} }

mydoc

=

mycol

.

find

(

myquery

)

for

x

in

mydoc

:

print

(

x

)

The output is:

{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'RUNOOB', 'alexa': '10000', 'url': 'https://www.runoob.com'}

Return a specified number of records

If we want to limit the query result to a specified number of records, we can use the limit() method, which accepts only a numeric argument.

The following example returns 3 documents:

Example

import

pymongo

myclient

=

pymongo

.

MongoClient

(

"

mongodb://localhost:27017/

"

)

mydb

=

myclient

[

"

runoobdb

"

]

mycol

=

mydb

[

"

sites

"

]

myresult

=

mycol

.

find

(

)

.

limit

(

3

)

Output result

for

x

in

myresult

:

print

(

x

)

The output is:

{'_id': ObjectId('5b23696ac315325f269f28d1'), 'name': 'RUNOOB', 'alexa': '10000', 'url': 'https://www.runoob.com'}
{'_id': ObjectId('5b2369cac315325f3698a1cf'), 'name': 'Google', 'alexa': '1', 'url': 'https://www.google.com'}
{'_id': ObjectId('5b236aa9c315325f5236bbb6'), 'name': 'Taobao', 'alexa': '100', 'url': 'https://www.taobao.com'}

Python Mongodb Python Mongodb