MongoDB basic operations in Python: Connecting, querying instances _python

Source: Internet
Author: User
Tags mongodb

MongoDB is a database based on distributed file storage. Written by the C + + language. A high-performance data storage solution designed to provide an exhibition for Web applications. It is characterized by high-performance, easy to deploy, easy to use, storage data is very convenient.

MongoDB Simple to use

Join database

Copy 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 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 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 [ten]: 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 '
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 [[]: db.connection//view join information
OUT[23]: Connection (' 192.168.1.3 ', 27017)

in [[]: Db.create_collection (' test_abeen ')//Create a 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 Code code as follows:

In [to]: Db.collection_names ()//View all collection names for the current database
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 [per]: 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 [+]: 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 is a test by Abeen"}
in [[]: Posts = db.posts
in [[]: Posts.insert (POST)//Insert document to database collection, default to create collection
OUT[40]: ObjectId (' 4c358492421aa91e70000000 ')
in [[]: Db.collection_names ()//Display 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 the collection
Out[42]:
{u ' _id ': ObjectId (' 4c358492421aa91e70000000 '),
  U ' author ': U ' Mike ',
 u ' text ': U ' this are a test by Abeen '}
in [?]: P.update ({"Author": "Mike"},{"$set": {"auth or ': ' Abeen ', ' text ': ' This is 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 ' is a test by Abeen Shan Shan '}]

In [%]: List (Posts.find ())
out[96]:
[{u ' _id ': ObjectId (' 4c358492421aa91e70000000 '),
  U ' author ': U ' Mike ',
  u ' text ': U ' is 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 are updated '}]
in [97] : Posts.remove ({"A": "Abeen"})//delete eligible documents
in [?]: List (Posts.find ())
Out[98]:
[{u ' _id ': ObjectId (' 4c358492421aa91e70000000 '),
  U ' author ': U ' Mike ',
  u ' text ': U ' is 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 '}]

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 [[]: Db.drop_collection ("Doc_abeen")//Delete collection
In [the]: 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 Code code as follows:

In [113]: result = Db.posts.find ({"A": "AA"})/Lookup
In [114]: type (Result)
OUT[114]: <class ' pymongo.cursor.Cursor ' >
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 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 Code code as follows:

In [£]: Db.posts.count ()//Current collection document Count
OUT[120]: 3
In [121]: type (db.posts)
OUT[121]: <class ' pymongo.collection.Collection ' >

In [138]: Posts.rename (' test_abeen ')//renaming the 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-side 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 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-null value
> Db.foo.find ({"Y": {$type: 10}})
{"_id": ObjectId ("4dc1975312c677fc83b5629f"), "X": 3, "Y": null}

Query #3 y results that do not exist
> 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.