python 蒙戈

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 蒙戈相关的知识,希望对你有一定的参考价值。

# 实际使用
# 信息检索
mongo_client = pymongo.MongoClient(
    'localhost', 228, connectTimeoutMS=500
)
mongo_client.database_names()
mongo_client.FMT_Tick_DB.collection_names()
mongo_client.Log.collection_names()
# 统计信息
mongo_db_stats = {}
for name in mongo_client.database_names():
    print name
    mongo_db_stats[name] = mongo_client[name].command(
        "dbstats", scale=1024 * 1024)
mongo_db_stats = pd.DataFrame(mongo_db_stats).T.loc[:,
    ['storageSize', 'collections', 'objects', 'avgObjSize']
]
mongo_client.Log['20181023'].find().sort([("time", pymongo.ASCENDING)].limit(50)
mongo_client.Log['20181023'].find({"message": {"$exists": True}})]


``` python
from pymongo import MongoClient
import pprint

client = MongoClient('localhost', 228)
db = client.test_database  # client['test-database']
collection = db.test_collection  # db['test-collection']
post = {'author': 'mike', 'text': 'My first post!'}

"""Inserting a document"""
posts = db.posts
post_id = posts.insert_one(post).inserted_id

"""Find a document"""
pprint.pprint(post.find_one())  #  matching the one that we inserted previously
pprint.pprint(post.find_one({"author": "Mike"}))

"""Bulk Inserts"""
new_posts = [{}, {}]
result = posts.insert_many(new_posts)

"""Querying for more than one doc"""
for post in posts.find():  # return cursor instance
    pprint.pprint(post)
for post in posts.find({"author": "Mike"}):  # return cursor instance
    pprint.pprint(post)

"""Count"""
posts.count()
posts.find({"author": "Mike"}).count()

"""Range Queries"""
d = datetime.datetime(2009, 11, 12, 12)
for post in posts.find({"date": {"$lt": d}}).sort("author"):
    pprint.pprint(post)

"""Indexing"""
result = db.profiles.create_index([('user_id', pymongo.ASCENDING)],
                                   unique=True)
sorted(list(db.profiles.index_information()))
```

```python
# CRUD
# Query
cursor = db.inventory.find({filter}, {projection})
cursor = db.inventory.find({})
cursor = db.inventory.find({"status": "A", "qty": {"$lt": 30}})
# SELECT * FROM inventory WHERE status = "A" AND qty < 30
cursor = db.inventory.find(
    {"$or": [{"status": "A"}, {"qty": {"$lt": 30}}]})
cursor = db.inventory.find({
    "status": "A",
    "$or": [{"qty": {"$lt": 30}}, {"item": {"$regex": "^p"}}]})
# SELECT * FROM inventory WHERE status = "A" OR qty < 30

# Array
cursor = db.inventory.find({"tags": ["red", "blank"]})
cursor = db.inventory.find({"tags": {"$all": ["red", "blank"]}})
cursor = db.inventory.find({"tags": "red"})

cursor = db.inventory.find({"dim_cm": {"$gt": 25}})

cursor = db.inventory.find({"dim_cm": {"$gt": 25}})
cursor = db.inventory.find({"dim_cm": {"$gt": 15, "$lt": 20}})
cursor = db.inventory.find({"dim_cm.1": {"$gt": 25}})
cursor = db.inventory.find({"tags": {"$size": 3}})

cursor = db.inventory.find(
    {"status": "A"}, {"status": 0, "instock": 0})
cursor = db.inventory.find(
    {"status": "A"}, {"item": 1, "status": 1, "size.uom": 1})
cursor = db.inventory.find({"status": "A"}, {"size.uom": 0})

cursor = db.inventory.find({"item": None})
```

# Operations
## Comparison
    - $eq, $ne
    - $gt, $lt
    - $gte, $lte
    - $in, $nin

## Logical
    - $and, $not, $nor, $or

## Element
    - $exists  # matches documents that have the specified field
    - $type    # select document if ta field is of specified type

## Evaluation
    - $expr
    - $jsonSchema
    - $mod
    - $regex
    - $text
    - $where

## Array
    - $all
    - $eleMatch
    - $size

## Projection
    - $
    - $elemMatch
    - $meta
    - $slice

## Bitwise
    - $bitsALlClear
    - $bitsAllSet

Filename with extension
  

    - $bitsAnyClear
    - $bitsAnySet

以上是关于python 蒙戈的主要内容,如果未能解决你的问题,请参考以下文章

nodejs:会话存储在哪里?啥是连接蒙戈?

text 蒙戈集团

javascript 节点蒙戈-addIndexing

javascript 蒙戈数据库

csharp 蒙戈 - repository.cs

csharp 蒙戈 - datacontext.cs