zoukankan      html  css  js  c++  java
  • Windows平台下为Python添加MongoDB支持PyMongo

    1. Python官网下载pymongo-2.6.3.win-amd64-py2.7.exe

    2. 安装pymongo-2.6.3.win-amd64-py2.7.exe

    3. 参照官方的用例进行测试

    • 打开命令提示符,进入Python运行环境。

    • 导入pymongo模块

      >>> import pymongo
    • 建立到本地MongoDB服务的链接

    • >>> client = pymongo.MongoClient("localhost", 27017)
    • 连接test数据库

    • >>> db = client.test
    • 查询连接的数据库名称

    • >>> db.name
      u'test'
    • 查询my_collection集合信息

    • >>> db.my_collection
      Collection(Database(MongoClient('localhost', 27017), u'test'), u'my_collection')
    • 向my_collection集合添加一些测试文档/对象

    • >>> db.my_collection.save({"x": 10})
      ObjectId('530034752052d502c4a250aa')>>> db.my_collection.save({"x": 8})
      ObjectId('5300347d2052d502c4a250ab')>>> db.my_collection.save({"x": 11})
      ObjectId('530034832052d502c4a250ac')
    • 在my_collection集合中查询一个文档/对象

    • >>> db.my_collection.find_one()
      {u'x': 10, u'_id': ObjectId('530034752052d502c4a250aa')}
    • 在my_collection集合中查询所有文档/对象,并遍历输出

    • 复制代码

      IndentationError: expected an indented block>>> for item in db.my_collection.find():
      ...     print item["x"]
      ...10811

      复制代码

    • 为my_collection集合创建一个索引

      >>> db.my_collection.create_index("x")
      u'x_1'
    • 在my_collection集合中查询所有文档/对象,并按升序遍历输出

      >>> for item in db.my_collection.find().sort("x", pymongo.ASCENDING):
      ...     print item["x"]
      ...81011
    • 在my_collection集合中查询所有文档/对象,并一定规则遍历输出

      >>> [item["x"] for item in db.my_collection.find().limit(2).skip(1)]
      [8, 11]
  • 相关阅读:
    git 学习笔记
    参看gitlab版本号
    PHP7.1安装xdebug
    言不由衷
    容器镜像上传和下载
    利用docker搭建ubuntu+nginx+PHP容器
    生产者消费者模式(转)
    白盒测试以及基路径法测试
    分页的简单实现
    排列2(全排列next_permutation 注意格式)
  • 原文地址:https://www.cnblogs.com/j6-2/p/4629677.html
Copyright © 2011-2022 走看看