📜  PyMongo 中的嵌套查询

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

PyMongo 中的嵌套查询

MongoDB是一个 NoSQL 面向文档的数据库。它对关系没有太大的重要性,或者也可以说它是无模式的。

PyMongo 是一个Python模块,可用于在 mongo 数据库和Python应用程序之间进行交互。 Python应用程序和 mongo 数据库之间交换的数据是二进制 JSON 格式。

PyMongo 中的嵌套查询

要从 MongoDB 文档中获取特定记录,查询起着重要作用。尽快获取正确的数据以做出正确的决定是必要的。以下是一些多重查询请求技术。

PyMongo 中的查询运算符

要使用$and$or$not MongoDB运算符,外部字典键必须是查询运算符之一;和字典参数必须在Python列表中,并且该Python列表必须是键的值。

句法 :

query = { 
          '$and' : [
               { operand_query_1},
               { operand_query_2} 
                   ]
         }

示例 1:创建一个名为讲师的集合并使用find()检索。

import pprint
from pymongo import MongoClient
  
  
client = MongoClient()
  
Database = client["GFG"]
lecturers = Database["lecture"]
  
lecturers.insert_many([
   {"l_id":56, "d_id":1, "salary":50000},
   {"l_id":57, "d_id":3, "salary":40000},
   {"l_id":58, "d_id":4, "salary":90000},
   {"l_id":59, "d_id":2, "salary":50000},
   {"l_id":52, "d_id":1, "salary":70000},
   {"l_id":53, "d_id":5, "salary":30000}
])
  
# retrieving the documents
for x in lecturers.find():
    pprint.pprint(x)

输出 :

查询一:显示工资低于50000的讲师记录,按升序排列。

# lecturer records with salary less 
# than 50000 and arrange in ascending order.
pprint.pprint(list(lecturers.find({"salary":
                                  {'$lt':50000}}).sort('salary', 1)))

输出 :

查询2:显示department_id 1中薪水大于40000的讲师记录,按薪水降序排列。

# lecturer records with salary greater than 40000
# in department_id 1 and sort according to their 
# salary in descending order.
pprint.pprint(list(lecturers.find({'$and':
                                   [{"d_id":1},
                                    {"salary":
                                     {'$gte':50000}}]}).sort("salary", -1)))

输出 :

示例 2:创建一个名为 books 的集合并使用find()进行检索。

import pprint
from pymongo import MongoClient
import datetime
  
  
client = MongoClient()
Database = client["GFG"]
books = Database["book"]
  
books.insert_many([
    {"author":"Samsuel", "book_id":54, "ratings":3,
     "publish":datetime.datetime(1999, 12, 6)},
      
    {"author":"Thomson", "book_id":84, "ratings":4,
     "publish":datetime.datetime(1996, 7, 12)},
      
    {"author":"Piyush Agarwal", "book_id":34, "ratings":1,
     "publish":datetime.datetime(2000, 9, 6)},
      
    {"author":"Shreya Mathur", "book_id":12, "ratings":2, 
     "publish":datetime.datetime(2017, 8, 8)},
      
    {"author":"Antony Sharma", "book_id":98, "ratings":4, 
     "publish":datetime.datetime(2003, 11, 5)},
])
  
# retrieving the documents
for x in books.find():
    pprint.pprint(x)

输出 :

查询1:显示2000年以后出版的评分大于3的书籍的记录。

# books with ratings greater than 3 published after 2000
pprint.pprint(list(books.find({'$and':
                               [{"ratings":
                                 {'$gt':3}},
                                {"publish":
                                 {'$gt':datetime.datetime(2000, 12, 31)
                                 }
                                }
                               ]
                              }
                             )
                  )
             )

输出 :

查询2:显示1999年至2016年出版的评分大于1的书籍的记录,按降序排列。

# between 1999-2016
query ={'$and':
        [{"publish":
          {'$gte':datetime.datetime(1999, 1, 1)}}, 
         {"publish":
          {'$lte':datetime.datetime(2016, 12, 31)}}]}
  
# books with ratings greater than 1
# and published between the year 
# 1999-2016, sort in decreasing order.
pprint.pprint(list(books.find({'$and':
                               [{"ratings":
                                 {'$gt':1}},
                                query]}).sort("publish", -1)))

输出 :