• Python mongoDB 的简单操作


    #!/usr/bin/env python
    # coding:utf-8
    # Filename:mongodb.py
    
    from pymongo import MongoClient,ASCENDING,DESCENDING
    import datetime
    
    # connection with mongoclient
    client=MongoClient()
    
    # getting a database
    db=client.test
    
    # getting a collection
    collection=db.posts
    
    # documents
    post={"author":"Mike",
          "test":"My first blog post!",
          "tags":["mongodb","python","pymongo"],
          "date":datetime.datetime.utcnow()
    }
    
    # inserting a document
    post_id=collection.insert(post)
    print 'posts id is:',post_id
    print 'collection_names is:',db.collection_names()
    
    # getting a single document
    doc=db.posts.find_one()
    print doc
    
    #query by objectId
    print 'query is:', db.posts.find_one({"_id":post_id})
    
    # querying for more than one doc
    for post in db.posts.find():
        print post
    
    # counting
    print 'total count is:',db.posts.count()
    
    # range queries
    d=datetime.datetime(2014,8,9,12)
    for post in db.posts.find({"date":{"$gt":d}}).sort("author"):
        print 'gt is:',post
    
    # Indexing
    # before indexing
    print db.posts.find({"date":{"$gt":d}}).sort("author").explain()["cursor"]
    print db.posts.find({"date":{"$gt":d}}).sort("author").explain()["nscanned"]
    # after indexing
    db.posts.create_index([("date",DESCENDING),("author",ASCENDING)])
    print db.posts.find({"date":{"$gt":d}}).sort("author").explain()["cursor"]
    print db.posts.find({"date":{"$gt":d}}).sort("author").explain()["nscanned"]
    
    # remove all indexes
    db.posts.drop_indexes()
  • 相关阅读:
    210
    209
    208
    207
    定时任务crontab
    Python的zip与*zip函数的应用
    Python的reduce函数与map函数
    解析:cpu与io密集在何场景适合使用多进程,多线程,协程
    Python上下文(转载)
    C10K与C10M的问题
  • 原文地址:https://www.cnblogs.com/ibgo/p/3901550.html
Copyright © 2020-2023  润新知