Create a connection
>>> Import pymongo
>>> Connection = pymongo. Connection ('localhost', 27017)
Switch Database
>>> Db = connection. test_database
Get collection
>>> Collection = db. test_collection
Db and collection are created in a delayed manner. They are created only when Document is added.
Add document, _ id automatically created
>>> Import datetime
>>> Post = {"author": "Mike ",
... "Text": "My first blog post! ",
... "Tags": ["mongodb", "python", "pymongo"],
... "Date": datetime. datetime. utcnow ()}
>>> Posts = db. posts
>>> Posts. insert (post)
ObjectId ('...')
Batch insert
>>> New_posts = [{"author": "Mike ",
... "Text": "Another post! ",
... "Tags": ["bulk", "insert"],
... "Date": datetime. datetime (2009, 11, 12, 11, 14 )},
... {"Author": "Eliot ",
... "Title": "MongoDB is fun ",
... "Text": "and pretty easy too! ",
... "Date": datetime. datetime (2009, 11, 10, 10, 45)}]
>>> Posts. insert (new_posts)
[ObjectId ('...'), ObjectId ('...')]
Get all collections (equivalent to SQL show tables)
>>> Db. collection_names ()
[U 'posts', u 'System. indexes']
Obtain a single document
>>> Posts. find_one ()
{U 'date': datetime. datetime (...), u 'text': u'my first blog post! ', U' _ id': ObjectId ('... '), u'author': u'mike', u 'tags': [u 'mongodb ', u 'python', u 'pymongo']}
Query multiple documents
>>For post in posts. find ():
... Post
...
{U 'date': datetime. datetime (...), u 'text': u'my first blog post! ', U' _ id': ObjectId ('... '), u'author': u'mike', u 'tags': [u 'mongodb ', u 'python', u 'pymongo']}
{U 'date': datetime. datetime (2009, 11, 12, 11, 14), u 'text': u'another post! ', U' _ id': ObjectId ('... '), u'author': u'mike', u 'tags': [u 'bulk', u'insert']}
{U 'date': datetime. datetime (2009, 11, 10, 10, 45), u 'text': U' and pretty easy too! ', U' _ id': ObjectId ('... '), u'author': u'eliot', u'title': u'mongodb is fun '}
Conditional Query
>>> Posts. find_one ({"author": "Mike "})
Advanced Query
>>> Posts. find ({"date": {"$ lt": d}). sort ("author ")
Count
>>> Posts. count ()
3
Add Index
>>> From pymongo import ASCENDING, DESCENDING
>>> Posts. create_index ([("date", DESCENDING), ("author", ASCENDING)])
U'date _-hour author_1'
View query statement Performance
>>> Posts. find ({"date": {"$ lt": d}). sort ("author"). explain () ["cursor"]
U'btreecursor date _-hour author_1'
>>> Posts. find ({"date": {"$ lt": d}). sort ("author"). explain () ["nscanned"]
2
Note that you should be careful with your summary for your reference only.
Disadvantages
Instead of replacing the entire tree in a traditional database (NoSQLFan: Can it replace an application scenario that needs to be viewed), it does not support complex transactions (NoSQLFan: MongoDB only supports atomic operations on a single document, not easy to search, 4 MB limit? (NoSQLFan: version 1.8 has been changed to 16 MB)
Features ):
In a document-based database, the table structure can be embedded with no mode, avoiding the overhead of null fields (Schema Free) distributed query support regular expressions dynamic expansion the 32-bit version can only store up to GB of data (NoSQLFan: the maximum file size is 2 GB, and 64-bit is recommended in the production environment)
Noun correspondence
A data item is called Document (NoSQLFan: corresponding to a single record in MySQL). One Document is embedded in another Document (comment Embedded post). The data stored in Embed is called Collections (NoSQLFan: corresponding to a table in MySQL) table Association, called Reference