MongoDB basic operations in Python: Connecting, querying instances

Source: Internet
Author: User
MongoDB is a database based on distributed file storage. Written by the C + + language. A high-performance data storage solution that is designed to provide a protective display for Web applications. It is characterized by high performance, easy to deploy, easy to use, and easy to store data.

MongoDB Simple to use

Join database

Copy the Code code as follows:


In [1]: Import Pymongo
In [2]: from Pymongo import Connection
In [3]: Connection = connection (' 192.168.1.3 ', 27017)//Create Join

Connection Related parameters

Copy the Code code as follows:


Connection ([host= ' localhost ' [, port=27017[, pool_size=none[, auto_start_request=none[, timeout=none[, slave_okay= false[, network_timeout=none[, document_class=dict[, Tz_aware=true]]]

Database operations

Copy the Code code as follows:


In [9]: C.database_names ()//list all database names
OUT[9]: [u ' Test ', u ' admin ', U ' yuhen ', U ' SMS ', U ' Local ']

In []: C.server_info ()//view server-related information
OUT[10]:
{u ' bits ': 64,
U ' gitversion ': U ' nogitversion ',
U ' OK ': 1.0,
U ' sysInfo ': U ' Linux yellow 2.6.24-27-server #1 SMP Fri Mar 01:23:09 UTC [x86_64 boost_lib_version=1_40 ',
U ' version ': U ' 1.2.2 '}

in [+]: db = c[' Test '//Select database
In []: Db.collection_names ()//list all collection names in the current database
OUT[17]: [u ' system.indexes ', U ' fs.files ', U ' fs.chunks ', U ' Test_gao ']

In [all]: db.connection//view join information
OUT[23]: Connection (' 192.168.1.3 ', 27017)

In []: Db.create_collection (' test_abeen ')//Create new collection
OUT[24]: Collection (Database (Connection (' 192.168.1.3 ', 27017), U ' Test '), U ' Test_abeen ')

in [+]: db.last_status ()//view last operation status
OUT[25]: {u ' err ': None, U ' n ': 0, U ' OK ': 1.0}

in [+]: db.name//view Current database name
OUT[26]: U ' test '

in [+]: Db.profiling_info ()//view configuration information
OUT[27]: []

In []: Db.profiling_level ()
OUT[28]: 0.0

Collection operations
Copy the Code code as follows:


In [to]: Db.collection_names ()//view the current database all collection names
OUT[31]:
[u ' system.indexes ',
U ' fs.files ',
U ' fs.chunks ',
U ' Test_gao ',
U ' system.users ',
U ' test_abeen ')

in [+]: c = db.test_abeen//Select Collection
in [[]: c.name//view current collection name
OUT[33]: U ' test_abeen '

in [+]: c.full_name//View the full name of the current collection
OUT[35]: U ' test.test_abeen '
In [approx]: c.database//view current collection database related information
OUT[36]: Database (Connection (' 192.168.1.3 ', 27017), U ' Test ')

in [+]: Post = {"Author": "Mike", "text": "This was a test by Abeen"}
In [all]: posts = db.posts
In [MAX]: Posts.insert (POST)//Insert document into database collection, create collection by default
OUT[40]: ObjectId (' 4c358492421aa91e70000000 ')
in [+]: db.collection_names ()//Show all collection names
OUT[41]:
[u ' system.indexes ',
U ' fs.files ',
U ' fs.chunks ',
U ' Test_gao ',
U ' system.users ',
U ' Test_abeen ',
U ' posts ')

In []: Posts.find_one ()//Find information from collection
Out[42]:
{u ' _id ': ObjectId (' 4c358492421aa91e70000000 '),
U ' author ': U ' Mike ',
U ' text ': U ' This was a test by Abeen '}
in [the]: P.update ({"Author": "Mike"},{"$set": {"author": "Abeen", " Text ":" This was a test by Abeen Shan Shan "}})//Update collection Document Information
in []: List (P.find ())
Out[55]:
[{u ' _id ': ObjectId (' 4c358492421aa91e70000000 '),
U ' author ': U ' abeen ',
U ' text ': U ' This was a test by Abeen Shan Shan '}]

In []: List (Posts.find ())
out[96]:
[{u ' _id ': ObjectId (' 4c358492421aa91e70000000 '),
U ' author ': U ' Mike ',
U ' text ': U ' This was a test by Abeen '},
{u ' _id ': ObjectId (' 4c358ad4421aa91e70000002 '), U ' a ': U ' AA ', U ' b ': U ' BB '},
{u ' _id ': ObjectId (' 4c358ad9421aa91e70000003 '), U ' a ': U ' AA ', U ' b ': U ' BB '},
{u ' _id ': ObjectId (' 4c358abb421aa91e70000001 '),
U ' a ': U ' abeen ',
U ' b ': U ' this bb was updated '}]
in []: Posts.remove ({"A": "Abeen" })//delete the qualified document
in [98]: List (Posts.find ())
Out[98]:
[{u ' _id ': ObjectId (' 4c358492421aa91e70000000 '),
U ' author ': U ' Mike ',
U ' text ': U ' This was a test by Abeen '},
{u ' _id ': ObjectId (' 4c358ad4421aa91e70000002 '), U ' a ': U ' A A ', U ' b ': U ' BB '},
{u ' _id ': ObjectId (' 4c358ad9421aa91e70000003 '), U ' a ': U ' AA ', U ' b ': U ' BB '}]

In [102]: Db.collection_names ()
OUT[102]:
[u ' system.indexes ',
U ' fs.files ',
U ' fs.chunks ',
U ' Test_gao ',
U ' system.users ',
U ' Test_abeen ',
U ' posts ',
U ' doc_abeen ')

In [104]: Db.drop_collection ("Doc_abeen")//Delete collection
In []: Db.collection_names ()
OUT[105]:
[u ' system.indexes ',
U ' fs.files ',
U ' fs.chunks ',
U ' Test_gao ',
U ' system.users ',
U ' Test_abeen ',
U ' posts ')

Code
Copy the Code code as follows:


In [113]: result = Db.posts.find ({"A": "AA"})//Lookup
In [All]: type (Result)
OUT[114]:
In [119]: List (result)
OUT[119]:
[{u ' _id ': ObjectId (' 4c358ad4421aa91e70000002 '), U ' a ': U ' AA ', U ' b ': U ' BB '},
{u ' _id ': ObjectId (' 4c358ad9421aa91e70000003 '), U ' a ': U ' AA ', U ' b ': U ' BB '}]

Find format
Copy the Code code as follows:


Find ([spec=none[, fields=none[, skip=0[, limit=0[, timeout=true[, snapshot=false[, tailable=false[, sort=None[, Max_ scan=none[, as_class=none[, **kwargs]]]]

Code
Copy the Code code as follows:


in [+]: Db.posts.count ()//current collection of documents
OUT[120]: 3
In [121]: type (db.posts)
OUT[121]:

In [138]: Posts.rename (' test_abeen ')//rename current collection
In [139]: Db.collection_names ()
OUT[139]:
[u ' system.indexes ',
U ' fs.files ',
U ' fs.chunks ',
U ' Test_gao ',
U ' system.users ',
U ' test_abeen ')

In [151]: for post in C.find ({"A": "AA"}). Sort ("a")://Query side-by sequence
Post
OUT[152]: {u ' _id ': ObjectId (' 4c358ad4421aa91e70000002 '), U ' a ': U ' AA ', U ' b ': U ' BB '}
OUT[152]: {u ' _id ': ObjectId (' 4c358ad9421aa91e70000003 '), U ' a ': U ' AA ', U ' b ': U ' BB '}

Copy the Code code as follows:


> Db.foo.insert ({x:1, y:1})
> Db.foo.insert ({x:2, y: "string"})
> Db.foo.insert ({x:3, y:null})
> Db.foo.insert ({x:4})

Query #1 y is null or does not exist
> Db.foo.find ({"Y": null})
{"_id": ObjectId ("4dc1975312c677fc83b5629f"), "X": 3, "Y": null}
{"_id": ObjectId ("4dc1975a12c677fc83b562a0"), "X": 4}

Query #2 y is a null value
> Db.foo.find ({"Y": {$type: 10}})
{"_id": ObjectId ("4dc1975312c677fc83b5629f"), "X": 3, "Y": null}

Query #3 y non-existent result
> Db.foo.find ({"Y": {$exists: false}})
{"_id": ObjectId ("4dc1975a12c677fc83b562a0"), "X": 4}

  • Related Article

    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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.