zoukankan      html  css  js  c++  java
  • python的mongo模块

    vi an_mongo.py

    #!/usr/bin/python3
    # -*- coding: utf-8- -*-
    
    import pymongo
    
    from pymongo import MongoClient
    
    client = MongoClient('localhost', 27017)
    
    db = client.pymongo_test
    
    posts = db.posts
    
    post_data = {
        'title': 'Python and MongoDB',
        'content': 'PyMongo is fun, you guys',
        'author': 'Scott'
    }
    result = posts.insert_one(post_data)
    print('One post: {0}'.format(result.inserted_id))

    mongo

    MongoDB shell version: 2.6.12
    connecting to: test
    > show databases;
    admin  (empty)
    cvedb  0.953GB
    local  0.078GB
    > show databases;
    admin         (empty)
    cvedb         0.953GB
    local         0.078GB
    pymongo_test  0.078GB
    > use pymongo_test
    switched to db pymongo_test
    > show tables;
    posts
    system.indexes
    > db.posts.find().count()
    1
    > db.posts.find().pretty()
    {
            "_id" : ObjectId("5a7973ed594c1009eb0a5c04"),
            "author" : "Scott",
            "content" : "PyMongo is fun, you guys",
            "title" : "Python and MongoDB"
    }
    > db.posts.find("author":"Scott")
    2018-02-06T17:25:40.885+0800 SyntaxError: Unexpected token :
    > db.posts.find('author':'Scott')
    2018-02-06T17:26:25.536+0800 SyntaxError: Unexpected token :
    > db.posts.find({'author':'Scott'})
    { "_id" : ObjectId("5a7973ed594c1009eb0a5c04"), "author" : "Scott", "content" : "PyMongo is fun, you guys", "title" : "Python and MongoDB" }
    > 

    mongo数据库是一个_id,然后跟上你的一串数据,以“,”做分割,看一个复杂一点的数据:

    { "_id" : ObjectId("5a77f788a4f2d63dbb53f84b"), "id" : "cpe:2.3:a:%240.99_kindle_books_project:%240.99_kindle_books:6:-:-:-:-:android", "title" : "$0.99 Kindle Books project $0.99 Kindle Books (aka com.kindle.books.for99) for android 6.0", "references" : [ "https://play.google.com/store/apps/details?id=com.kindle.books.for99", "https://docs.google.com/spreadsheets/d/1t5GXwjw82SyunALVJb2w0zi3FoLRIkfGPc7AMjRF0r4/edit?pli=1#gid=1053404143" ], "cpe_2_2" : "cpe:/a:%240.99_kindle_books_project:%240.99_kindle_books:6::~~~android~~" }

    mongo库中的状态是:

    {
            "_id" : ObjectId("5a77f788a4f2d63dbb53f84b"),
            "id" : "cpe:2.3:a:%240.99_kindle_books_project:%240.99_kindle_books:6:-:-:-:-:android",
            "title" : "$0.99 Kindle Books project $0.99 Kindle Books (aka com.kindle.books.for99) for android 6.0",
            "references" : [
                    "https://play.google.com/store/apps/details?id=com.kindle.books.for99",
                    "https://docs.google.com/spreadsheets/d/1t5GXwjw82SyunALVJb2w0zi3FoLRIkfGPc7AMjRF0r4/edit?pli=1#gid=1053404143"
            ],
            "cpe_2_2" : "cpe:/a:%240.99_kindle_books_project:%240.99_kindle_books:6::~~~android~~"
    }

    mongo刚接触2天,还不是特别熟悉。

  • 相关阅读:
    css数学运算函数 calc(),和css的数学运算
    MySQL设置字段的默认值为当前系统时间
    今天阿里云服务器被挂马wnTKYg挖矿的清理
    linux shell常用命令
    无损扩容,调整Centos6.5服务器分区大小,不适用centos7,centos6.5 调整逻辑卷大小
    添加远程库
    interface 设置默认值
    radio根据value值动态选中
    获取下拉js 具体值
    mysql中int、bigint、smallint 和 tinyint的存储
  • 原文地址:https://www.cnblogs.com/zw2002/p/8423313.html
Copyright © 2011-2022 走看看