📜  Python MongoDB – 查询

📅  最后修改于: 2022-05-13 01:55:41.542000             🧑  作者: Mango

Python MongoDB – 查询

MongoDB 是一个跨平台的面向文档和非关系(即NoSQL)的数据库程序。它是一个开源文档数据库,以键值对的形式存储数据。

什么是 MongoDB 查询?

MongoDB 查询用于使用查询运算符指定选择过滤器,同时通过 db.find() 方法从集合中检索数据。我们可以使用查询对象轻松过滤文档。要对集合应用过滤器,我们可以将指定所需文档的条件的查询作为参数传递给此方法,这是 db.find() 方法的可选参数。查询选择器:以下是 MongoDB 查询中使用的一些运算符的列表。

.math-table { 边框折叠:折叠;宽度:100%; } .math-table td { 边框:1px 实心 #5fb962;文本对齐:左!重要;填充:8px; } .math-table th { 边框:1px 实心 #5fb962;填充:8px; } .math-table tr>th{ 背景颜色:#c6ebd9;垂直对齐:中间; } .math-table tr:nth-child(odd) { background-color: #ffffff; }

OperationSyntaxDescription
Equality{“key” : “value”}Matches values that are equal to a specified value.
Less Than{“key” :{$lt:”value”}}Matches values that are less than a specified value.
Greater Than{“key” :{$gt:”value”}}Matches values that are greater than a specified value.
Less Than Equal to{“key” :{$lte:”value”}}Matches values that are less than or equal to a specified value.
Greater Than Equal to{“key” :{$lte:”value”}}Matches values that are greater than or equal to a specified value.
Not Equal to{“key”:{$ne: “value”}}Matches all values that are not equal to a specified value.
Logical AND{ “$and”:[{exp1}, {exp2}, …, {expN}] }Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.
Logical OR{ “$or”:[{exp1}, {Joins query clauses with a logical OR returns all documents that match the conditions of either clause.
Logical NOT{ “$not”:[{exp1}, {exp2}, …, {expN}] }Inverts the effect of a query expression and returns documents that do not match the query expression.

我们操作的数据库或集合: 示例 1:

Python3
# importing Mongoclient from pymongo
from pymongo import MongoClient
 
 
# Making Connection
myclient = MongoClient("mongodb://localhost:27017/")
  
# database
db = myclient["mydatabase"]
  
# Created or Switched to collection
# names: GeeksForGeeks
Collection = db["GeeksForGeeks"]
 
# Filtering the Quantities greater
# than 40 using query.
cursor = Collection.find({"Quantity":{"$gt":40}})
 
# Printing the filtered data.
print("The data having Quantity greater than 40 is:")
for record in cursor:
    print(record)
     
# Filtering the Quantities less
# than 40 using query.
cursor = Collection.find({"Quantity":{"$lt":40}})
 
# Printing the filtered data.
print("\nThe data having Quantity less than 40 is:")
for record in cursor:
    print(record)


Python3
# importing Mongoclient from pymongo
from pymongo import MongoClient
 
 
# Making Connection
myclient = MongoClient("mongodb://localhost:27017/")
  
# database
db = myclient["mydatabase"]
  
# Created or Switched to collection
# names: GeeksForGeeks
Collection = db["GeeksForGeeks"]
 
# Filtering the (Quantities greater than
# 40 AND greater than 40) using AND query.
cursor = Collection.find({"$and":[{"Quantity":{"$gt":40}},
                                  {"Quantity":{"$gt":50}}]})
 
# Printing the filtered data.
print("Quantities greater than 40 AND\
Quantities greater than 40 :")
for record in cursor:
    print(record)
 
# Filtering the (Quantities greater than
# 40 OR greater than 40) using OR query.
cursor = Collection.find({"$or":[{"Quantity":{"$gt":40}},
                                 {"Quantity":{"$gt":50}}]})
 
# Printing the filtered data.
print()
print("Quantities greater than 40 OR\
Quantities greater than 40 :")
for record in cursor:
    print(record)


输出: 示例 2:

Python3

# importing Mongoclient from pymongo
from pymongo import MongoClient
 
 
# Making Connection
myclient = MongoClient("mongodb://localhost:27017/")
  
# database
db = myclient["mydatabase"]
  
# Created or Switched to collection
# names: GeeksForGeeks
Collection = db["GeeksForGeeks"]
 
# Filtering the (Quantities greater than
# 40 AND greater than 40) using AND query.
cursor = Collection.find({"$and":[{"Quantity":{"$gt":40}},
                                  {"Quantity":{"$gt":50}}]})
 
# Printing the filtered data.
print("Quantities greater than 40 AND\
Quantities greater than 40 :")
for record in cursor:
    print(record)
 
# Filtering the (Quantities greater than
# 40 OR greater than 40) using OR query.
cursor = Collection.find({"$or":[{"Quantity":{"$gt":40}},
                                 {"Quantity":{"$gt":50}}]})
 
# Printing the filtered data.
print()
print("Quantities greater than 40 OR\
Quantities greater than 40 :")
for record in cursor:
    print(record)

输出: