Python operation MongoDB Partial translation

Source: Internet
Author: User
Tags bulk insert mongoclient mongodb server

Python Operation MongoDB

Http://api.mongodb.org/python/current/index.html

This tutorial is intended as a introduction to working with MongoDB and Pymongo.

prerequisites[Prerequisites]

Before we start, make sure and the Pymongo distribution installed. In the Python shell, the following should run without raising an exception:

>>> Import Pymongo

This tutorial also assumes, a MongoDB instance is running on the default host and port. Assuming you are downloaded and installed MongoDB, you can start it like so:

$ mongod
Making a Connection with mongoclient[connection]

The first step when working with Pymongo are to create a mongoclient to the running Mongod instance. Doing are easy:

>>> from Pymongo import mongoclient>>> client = Mongoclient ()

The above code would connect on the default host and port. We can also specify the host and port explicitly, as follows:

[The above code will connect to the default host and port.] We can also specify the host and port, as follows:]

Client = mongoclient (' localhost ', 27017)

Or Use the MongoDB URI format:

>>> client = mongoclient (' mongodb://localhost:27017/')
Getting a database[specified database]

A Single instance of MongoDB can support multiple independent databases. When working with Pymongo you access databases using attribute style access on Mongoclient instances:

[...... Attribute style ...]

>>> db = Client.test_database

If your database name is such this using attribute style access won ' t work (like test-database), you can use dictionary s Tyle Access instead:

[... Dictionary style ...]

>>> db = client [' Test-database ']

Supplemental, with password when used:

Db.authenticate ("User name", "password")
Getting a collection[specified collection]

A collection is a group of documents stored in MongoDB, and can being thought of as roughly the equivalent of a table in a re Lational database. Getting a collection in Pymongo works the same as Getting a database:

[... Can be imagined as being a table ... Like the previous section of the way]

>>> collection = Db.test_collection

or (using dictionary style access):

>>> collection = db [' Test-collection ']

An important note on collections (and databases) in MongoDB was that they be created lazily-none of the above command S has actually performed any operations on the MongoDB server. Collections and databases was created when the first document was inserted into them.

[... Create is delayed ... Not created until data is inserted)

documents[file]

The Data in MongoDB is represented (and stored) using Json-style documents. In Pymongo we use dictionaries to represent documents. As an example, the following dictionary might is used to represent a blog post:

[... JSON-style ... In Pymongo we use a dictionary to represent the document ...]

>>> Import datetime>>> post = {"Author": "Mike",... "Text": "My First blog post!",... "Tags": ["MongoDB", "Python", "Pymongo"],... "Date": DateTime. Datetime. UtcNow ()}

Note that documents can contain native Python types (like Datetime.datetime instances) which would be automatically convert Ed to and from the appropriate BSON types.

[... Can contain Python type, will automatically convert ... TODO BSON] ... Concept Bson () is a binary form of a class JSON storage format ... However, Bson has some data types that JSON does not have, such as date and bindata types.

Inserting a document[insert]

To insert a document into a collection we can use the Insert_one () method:

>>> posts = db.posts>>> post_id = Posts.insert_one (POST). Inserted_id>>> post_idobjectid (' ... ')

When a document is inserted a special key, "_id", was automatically added if the document doesn ' t already contain an "_id" Key. The value of "_id" must be unique across the collection. Insert_one () Returns an instance of Insertoneresult. For more information in "_id", see the documentation on _ID.

[... "_id" will automatically insert a, ............. Must be unique]

After inserting the first document, the posts collection have actually been created on the server. We can verify the listing all of the collections in our database:

[After inserting the first document, the collection ... Create. We can ... List all: Verify this:]

>>> db.collection_names (include_system_collections = False) [u ' posts ']
Getting a single document with Find_one () [Find_one get individual documents]

The most basic type of query, can be performed in MongoDB is Find_one (). This method returns a single document matching a query (or None if there is no matches). It is useful if you know there was only one matching document, or was only interested in the first match. Here we use Find_one () to get the first document from the posts collection:

[... When you know only one matching file, or just care about the first ...]

>>> 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 '}

The result is a dictionary matching, the one that we inserted previously.

Note The returned document contains an "_id", which is automatically added on insert.

Find_one () also supports querying on specific elements that the resulting document must match. To limit our results to a document with author "Mike" We do:

[... Specify criteria ...]

>>> Posts.find_one ({"Author": "Mike"}) {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 '}

If we try with a different author, like "Eliot", we ll get no result:

>>> posts. Find_one ({"Author": "Eliot"}) >>> None
Querying by objectid[based on ObjectId query]

We can also find a post by it _id, which in our example are an ObjectId:

>>> Post_idobjectid (...) >>> posts. Find_one ({"_id": post_id}) {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 '}

Note that a ObjectId is isn't the same as its string representation:

[... ObjectId ... Not a character type ...]

>>> post_id_as_str = str (post_id) >>> posts. Find_one ({"_id": Post_id_as_str}) # No result>>> None

A Common task in Web applications was to get A ObjectId from the request URL and find the matching document. It ' s necessary in this case to convert the ObjectId from a string before passing it to Find_one:

[............ Convert from string to Objectid ...]

From Bson.objectid import objectid# the web framework gets post_id from the URL and passes it as a stringdef get (post_id ):    # Convert from string to ObjectId:    document = Client.db.collection.find_one ({' _id ': ObjectId (post_id)})
A Note on Unicode strings[unicode string]

You probably noticed this regular Python strings we stored earlier look different when retrieved from the server (eg u ' Mike ' instead of ' Mike '). A short explanation are in order.

MongoDB stores data in BSON format. BSON strings is UTF-8 encoded so Pymongo must ensure this any strings it stores contain only valid UTF-8 data. Regular strings () is validated and stored unaltered. Unicode strings () is encoded UTF-8 first. The reason our example string are represented in the Python shell as U ' Mike ' instead of ' Mike ' are that Pymongo decodes each BSON string to a Python Unicode string, not a regular str.

Bulk inserts[BULK INSERT]

In order to make querying a little more interesting, let's insert a few more documents. In addition to inserting a single document, we can also perform bulk insert operations, by passing a list as the first arg Ument to Insert_many (). This would insert each document in the list, and sending only a single command to the server:

>>> new_posts = [{"Author": "Mike",... "Text": "Another post!",... "Tags": ["Bulk", "insert"],... "Date": DateTime. DateTime (one, one, one, one)},... {"Author": "Eliot",... "title": "MongoDB is Fun",... "Text": "and Pretty easy Too!",... "Date": DateTime. DateTime (All, one, ten, ten)}]>>> result = Posts.insert_many (new_posts) >>> result.inserted_i Ds[objectid (' ... '), ObjectId (' ... ')]

There is a couple of interesting things to note on this example:

    • The result from Insert_many () now returns-ObjectId instances, one for each inserted document.
    • NEW_POSTS[1] have a different "shape" than the other posts-there is no "tags" field and we ' ve added a new field, "title" . This is the what we mean when we say the. MongoDB is Schema-free. [Two structure is not the same, so that is Schema-free]
Querying for more Than one document[multiple queries]

To get more than a single document as the result of a query we use the Find () method. Find () returns a Cursor instance, which allows us to iterate through all matching documents. For example, we can iterate over every document in the posts collection:

[... find () ... Method... Result traversal ...]

>>> 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 (one, one, one, one, one), U ' text ': U ' another post! ', U ' _id ': O Bjectid (' ... '), U ' author ': U ' Mike ', U ' tags ': [u ' bulk ', u ' insert ']}{u ' date ': Datetime.datetime (one, one, ten, ten), U ' te XT ': U ' and pretty easy too! ', U ' _id ': ObjectId (' ... '), U ' author ': U ' Eliot ', U ' title ': U ' MongoDB is fun '}

Just like we do with Find_one (), we can pass a document to find () to limit the returned results. Here, we get only those documents whose author are "Mike":

[... Add condition, check multiple results ...]

>>> for post in Posts.find ({"Author": "Mike"}): ... 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 (one, one, one, one, one), U ' text ': U ' another post! ', U ' _id ': O Bjectid (' ... '), U ' author ': U ' Mike ', U ' tags ': [u ' bulk ', u ' Insert ']}
counting[Count]

If We just want to know what many documents match a query we can perform a count () operation instead of a full query. We can get a count of all of the documents in a collection:

>>> Posts.count () 3

Or just of those documents that match a specific query:

[.. Count of additional conditions ...]

>>> Posts.find ({"Author": "Mike"}). Count () 2
Range Queries

MongoDB supports many different types of advanced queries. As an example, lets perform a query where we limit results to posts older than a certain date, but also sort the results b Y Author:

[... Advanced Query ... Like what...... Time ...]

>>> d = datetime.datetime (one, one, and one) >>> for post in posts. Find ({"date": {"$lt":d}}). Sort ("author"): ... print post ... {u ' date ':  datetime.datetime (All, one, ten, ten, $), U ' text ': U ' and pretty easy too! ', U ' _id ': ObjectId (' ... '), U ' author ': U ' Eliot ', U ' title ': U ' MongoDB is fun '} {u ' date ': Datetime.datetime (one, one, one, one, +), U ' text ': U ' another post! ', U ' _id ': ObjectId (' ... '), U ' author ': U ' Mike ', U ' tags ': [u ' bulk ', u ' Insert ']}

Here we use the special "$LT" operator-do-a range query, and also call sort () to sort the results by author.

[... "$lt" operator, for range ...]

indexing[Index]

The above query fast we can add a compound index on "date" and "Author". To start, lets with the explain () method to get some information the "what" is being performed without the index:

>>> posts. Find ({"date": {"$lt": D}}). Sort ("Author"). Explain () ["Cursor"]u ' basiccursor ' >>> posts. Find ({"date": {"$lt": D}}). Sort ("Author"). Explain () ["Nscanned"]3

We can see that the query was using the basiccursor and scanning over all 3 documents in the collection. Now let's add a compound index and look at the same information:

[... Use Basiccursor ... Scanned 3 ... Let's look at the index after adding ...]

>>> from Pymongo Import Ascending, descending>>> posts. Create_index ([("Date", descending), ("Author", Ascending)]) u ' date_-1_author_1 ' >>> posts. Find ({"date": {"$lt": D}}). Sort ("Author"). Explain () ["Cursor"]u ' btreecursor date_-1_author_1 ' >>> posts. Find ({"date": {"$lt": D}}). Sort ("Author"). Explain () ["Nscanned"]2

Now the query was using a btreecursor (the index) and only scanning over the 2 matching documents.

Python operation MongoDB Partial translation

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.