当前位置: 首页 > 工具软件 > PyMongo > 使用案例 >

pymongo2.py

公羊曜灿
2023-12-01
"""
sort(key_or_list, direction=None) pymongo.cursor.Cursor实例的方法。
    对光标的结果进行排序。
    传递一个字段名和一个方向。
    direction: pymongo.ASCENDING(升序,默认)或者pymongo.DESCENDING(降序)。
        for doc in collection.find().sort('field', pymongo.ASCENDING):
            print(doc)

    按多个字段排序, 传递(key, direction)对的列表。
        for doc in collection.find().sort([
                ('field1', pymongo.ASCENDING),
                ('field2', pymongo.DESCENDING)]):
            print(doc)

    MongoDB版本2.6开始, 文本搜索结果可以按相关性排序:
        cursor = db.test.find(
            {'$text': {'$search': 'some words'}},
            {'score': {'$meta': 'textScore'}})
        # 按'score'字段排序。
        cursor.sort([('score', {'$meta': 'textScore'})])
        for doc in cursor:
            print(doc)

    Raises :class:`~pymongo.errors.InvalidOperation` if this cursor has
    already been used. Only the last :meth:`sort` applied to this
    cursor has any effect.
    :参数:
      - `key_or_list`: 一个字段或 一个(key, direction)对的列表,来指定排序的多个字段。
      - `direction` (可选): 只有在`key_or_list` 是一个字段时可用,
      如果未指定,默认使用pymongo.ASCENDING(升序)。
"""
import pymongo
from pymongo import MongoClient
from pprint import pprint

client = MongoClient()
db = client.test
# 条件查询、显示。
documents = db.posts.find(
    {
        "$and": [
            {"tags": {"$size": 2}},
            {"tags": {"$all": ["python", "pymongo"]}}
        ]
    },
    {"_id": 0, "text": 1, "tags": 1}
)
# 格式化打印。
for post in documents:
    pprint(post)
# 条件计数。
print(db.posts.find({"tags": {"$size": 3}}).count())
# 按likes字段降序排序,打印。
for post in db.posts.find({}, {"_id": 0}).sort("likes", pymongo.DESCENDING):
    pprint(post)
print()
for post in db.posts.find({"likes": {"$gt": 15}}).sort("author"):
    pprint(post)
print()
db.posts.update_one({"author": "Jimmy"}, {"$set": {"text": "revised post"}})
print(db.posts.find_one({"author": "Jimmy"}, {"_id": 0}))
print()
print(db.posts.find_one({"author": "Mike"}, {"_id": 0}))
db.posts.update_one({"author": "Mike"}, {"$inc": {"likes": 3}})
print(db.posts.find_one({"author": "Mike"}, {"_id": 0}))
print()
print(db.posts.find_one({"author": "Emily"}, {"_id": 0}))
db.posts.update_one({"author": "Emily"}, {"$pull": {"tags": "python3"}})
db.posts.update_one({"author": "Emily"}, {"$push": {"tags": "python3"}})
# db.posts.update_one({"author": "Emily"}, {"$pop": {"tags": -1}})
print(db.posts.find_one({"author": "Emily"}, {"_id": 0}))
print()
for post in db.posts.find({}, {"_id": 0}):
    pprint(post)
db.posts.update_many({}, {"$pull": {"tags": "database"}})
for post in db.posts.find({}, {"_id": 0}):
    pprint(post)
print()
# tags字段包含python的。
for p in db.posts.find({"tags": {"$in": ["python"]}}, {"_id": 0}):
    pprint(p)
db.posts.update_many({"tags": {"$in": ["python"]}}, {"$inc": {"likes": 5}})
for p in db.posts.find({"tags": {"$in": ["python"]}}, {"_id": 0}):
    pprint(p)
print()
for p in db.posts.find({"likes": {"$gt": 20}}, {"_id": 0}):
    pprint(p)
print(db.posts.find({"likes": {"$gt": 20}}).count())
print()
for p in db.posts.find({}, {"_id": 0}):
    pprint(p)
db.posts.delete_one({"author": "Emily"})
for p in db.posts.find({}, {"_id": 0}):
    pprint(p)
print()
for p in db.posts.find({}, {"_id": 0}):
    pprint(p)
db.posts.delete_many({"likes": {"$lt": 100}})
for p in db.posts.find({}, {"_id": 0}):
    pprint(p)
 类似资料: