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天,还不是特别熟悉。

  • 相关阅读:
    root用户javac可以执行sudo后command not found问题
    机器学习 KNN算法实现 (鸢尾花)
    机器学习 KNN分类算法简单介绍+数据集拆分
    机器学习的一般流程
    机器学习算法的性能评价
    Opencv 自带函数(Haar)的人脸检测
    Opencv 的基础认识
    labelme 的学习
    天梯赛总结CCCC
    VScode配置c,c++编译环境
  • 原文地址:https://www.cnblogs.com/zw2002/p/8423313.html
Copyright © 2011-2022 走看看