|
1 |
| -.. uses tutorial.rst |
| 1 | +.. _pymongo-insert: |
2 | 2 |
|
3 |
| -Inserting a Document |
4 |
| --------------------- |
5 |
| -To insert a document into a collection we can use the |
6 |
| -the ``~pymongo.collection.Collection.insert_one`` method method: |
| 3 | +================ |
| 4 | +Insert Documents |
| 5 | +================ |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: add, save |
| 19 | + |
| 20 | +In this guide, you can learn how to use {+driver-short+} to insert data into a |
| 21 | +MongoDB collection. |
| 22 | + |
| 23 | +Insert One |
| 24 | +---------- |
| 25 | + |
| 26 | +To insert a document into a collection, use the |
| 27 | +``~pymongo.collection.Collection.insert_one()`` method: |
7 | 28 |
|
8 | 29 | .. code-block:: python
|
9 | 30 |
|
10 |
| - >>> posts = db.posts |
11 |
| - >>> post_id = posts.insert_one(post).inserted_id |
12 |
| - >>> post_id |
13 |
| - ObjectId('...') |
| 31 | + >>> posts = db.posts |
| 32 | + >>> post_id = posts.insert_one(post).inserted_id |
| 33 | + >>> post_id |
| 34 | + ObjectId('...') |
14 | 35 |
|
15 |
| -When a document is inserted a special key, ``"_id"``, is automatically |
16 |
| -added if the document doesn't already contain an ``"_id"`` key. The value |
17 |
| -of ``"_id"`` must be unique across the |
18 |
| -collection. the ``~pymongo.collection.Collection.insert_one`` method returns an |
19 |
| -instance of ``~pymongo.results.InsertOneResult``. For more information |
20 |
| -on ``"_id"``, see the `documentation on _id |
21 |
| -<https://www.mongodb.com/docs/manual/reference/method/ObjectId/>`_. |
| 36 | +When you insert a document, the driver automatically adds an ``_id`` field, if |
| 37 | +the document doesn't already contain one. The value |
| 38 | +of ``_id`` must be unique across the collection. The |
| 39 | +``insert_one()`` method returns an instance of |
| 40 | +``~pymongo.results.InsertOneResult``. |
22 | 41 |
|
23 |
| -After inserting the first document, the *posts* collection has |
24 |
| -actually been created on the server. We can verify this by listing all |
25 |
| -of the collections in our database: |
| 42 | +When you insert the first document, MongoDB automatically creates the |
| 43 | +``posts`` collection on the server. You can verify this by listing all |
| 44 | +of the collections in the database: |
26 | 45 |
|
27 | 46 | .. code-block:: python
|
28 | 47 |
|
29 |
| - >>> db.list_collection_names() |
30 |
| - ['posts'] |
| 48 | + >>> db.list_collection_names() |
| 49 | + ['posts'] |
31 | 50 |
|
32 | 51 | Bulk Inserts
|
33 | 52 | ------------
|
34 |
| -In order to make querying a little more interesting, let's insert a |
35 |
| -few more documents. In addition to inserting a single document, we can |
36 |
| -also perform *bulk insert* operations, by passing a list as the |
37 |
| -first argument to the ``~pymongo.collection.Collection.insert_many`` method. |
38 |
| -This will insert each document in the list, sending only a single |
39 |
| -command to the server: |
| 53 | + |
| 54 | +In addition to inserting a single document, you can |
| 55 | +also perform bulk insert operations by passing a list as the |
| 56 | +first argument to the ``~pymongo.collection.Collection.insert_many()`` method. |
| 57 | +This sends a single command to the server to insert each document in the list. |
40 | 58 |
|
41 | 59 | .. code-block:: python
|
42 | 60 |
|
43 |
| - >>> new_posts = [ |
44 |
| - ... { |
45 |
| - ... "author": "Mike", |
46 |
| - ... "text": "Another post!", |
47 |
| - ... "tags": ["bulk", "insert"], |
48 |
| - ... "date": datetime.datetime(2009, 11, 12, 11, 14), |
49 |
| - ... }, |
50 |
| - ... { |
51 |
| - ... "author": "Eliot", |
52 |
| - ... "title": "MongoDB is fun", |
53 |
| - ... "text": "and pretty easy too!", |
54 |
| - ... "date": datetime.datetime(2009, 11, 10, 10, 45), |
55 |
| - ... }, |
56 |
| - ... ] |
57 |
| - >>> result = posts.insert_many(new_posts) |
58 |
| - >>> result.inserted_ids |
59 |
| - [ObjectId('...'), ObjectId('...')] |
60 |
| - |
61 |
| -There are a couple of interesting things to note about this example: |
62 |
| - |
63 |
| -- The result from the ``~pymongo.collection.Collection.insert_many`` method now |
64 |
| - returns two ``~bson.objectid.ObjectId`` instances, one for |
65 |
| - each inserted document. |
66 |
| -- ``new_posts[1]`` has a different "shape" than the other posts - |
67 |
| - there is no ``"tags"`` field and we've added a new field, |
68 |
| - ``"title"``. This is what we mean when we say that MongoDB is |
69 |
| - *schema-free*. |
| 61 | + >>> new_posts = [ |
| 62 | + ... { |
| 63 | + ... "author": "Mike", |
| 64 | + ... "text": "Another post!", |
| 65 | + ... "tags": ["bulk", "insert"], |
| 66 | + ... "date": datetime.datetime(2009, 11, 12, 11, 14), |
| 67 | + ... }, |
| 68 | + ... { |
| 69 | + ... "author": "Eliot", |
| 70 | + ... "title": "MongoDB is fun", |
| 71 | + ... "text": "and pretty easy too!", |
| 72 | + ... "date": datetime.datetime(2009, 11, 10, 10, 45), |
| 73 | + ... }, |
| 74 | + ... ] |
| 75 | + >>> result = posts.insert_many(new_posts) |
| 76 | + >>> result.inserted_ids |
| 77 | + [ObjectId('...'), ObjectId('...')] |
| 78 | + |
| 79 | +The ``insert_many()`` method returns an instance of ``~bson.objectid.ObjectId`` |
| 80 | +for each inserted document. |
| 81 | + |
| 82 | +.. note:: |
| 83 | + |
| 84 | + Because MongoDB is schema-free, ``new_posts[1]`` has different fields than |
| 85 | + other posts in the collection. It doesn't have a ``tags`` field, and |
| 86 | + has an added ``title`` field. |
0 commit comments