[Mongo] install and use PyMongo, mongopymongo

Source: Internet
Author: User

[Mongo] install and use PyMongo, mongopymongo
Install and use PyMongo

Here is a simple installation and usage record. First, you must have an available mongo environment, either win or linux environment. Suppose you have some knowledge about mongo and some command line operations.

Installation and update

Like installing most py packages, you can install them through source code or pip or easy_install.

  • Install

    Pip install pymongo

  • Upgrade

    Pip install -- upgrade pymongo

  • For other installation methods, see pymongo.

Operation

Official website tutorial

Small Cases
#-*-Coding: UTF-8-*-# python2.7x # author: orangleliu @ 2014-09-24 ''' simple usage of pymongo ''' from pymongo import using clientdef get_db (): # establish a connection client = clients client ("localhost", 27017) # test, and write other statements db = client. test return dbdef get_collection (db): # select a collection (collection and database in mongo are created by lazy. For details, refer to google) collection = db ['posts'] print collectiondef insert_one_doc (db): # insert a document posts = db. posts post = {"name": "lzz", "age": 25, "weight": "55"} post_id = posts. insert (post) print post_iddef insert_mulit_docs (db): # insert documents in batches and insert an array posts = db. posts post = [{"name": "nine", "age": 28, "weight": "55" },{ "name": "jack ", "age": 25, "weight": "55"}] obj_ids = posts. insert (post) print obj_ids # query, which can be used to query the entire set, root ObjectId query, def get_all_colls (db) such as a field query ): # obtain print db for all set names in a database. collection_names () def get_one_doc (db): # If yes, one is returned. If no, None posts = db is returned. posts print posts. find_one () print posts. find_one ({"name": "jack"}) print posts. find_one ({"name": "None"}) returndef get_one_by_id (db): # Use objectid to find a doc posts = db. posts obj = posts. find_one () obj_id = obj ["_ id"] print "_ id: ObjectId type:" print posts. find_one ({"_ id": obj_id}) # note that the obj_id here is an object rather than a str, the record print "_ id is str type" print posts. find_one ({"_ id": str (obj_id)}) # You can use the ObjectId method to convert str to the ObjectId type from bson. objectid import ObjectId print "_ id to ObjectId type" print posts. find_one ({"_ id": ObjectId (str (obj_id)}) def get_many_docs (db): # mongo provides a method for filtering and searching, you can obtain the dataset by filtering the # conditions, and process the data such as Count and sort. posts = db. posts # all data, sorted by age,-1 is descending order all = posts. find (). sort ("age",-1) count = posts. count () print "% s of all data in the Set" % int (count) for I in all: print I # conditional Query count = posts. find ({"name": "lzz "}). count () print "lzz: % s" % count for I in posts. find ({"name": "lzz", "age": {"$ lt": 20}): print idef clear_coll_datas (db ): # Clear all data databases in a set. posts. remove ({}) if _ name _ = "_ main _": db = get_db () obj_id = insert_one_doc (db) obj_ids = insert_mulit_docs (db) # get_all_colls (db) # get_one_doc (db) # get_one_by_id (db) # get_many_docs (db) clear_coll_datas (db)

This is a simple write operation. As for Set Operations and group operations, we will summarize them later.

This article is from the "orangleliu notebook" blog, please be sure to keep this http://blog.csdn.net/orangleliu/article/details/39545751
Mongo cannot access the command line during linux Installation

Connecting to: test

It indicates that it has been connected to the collection "test.
If the command prompt is not displayed, reinstall an earlier version.
 
I cannot open the kernel vue. I need NetFrame but I don't know which version to install. Please advise.

The computer was originally win7 32, and later changed to 64. As a result, the re-installed kernel vue could not be started. If you click the start icon, there will be no changes... how can you solve this problem?
 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.