diff --git a/source/includes/steps-test-generate-multiple-documents.yaml b/source/includes/steps-test-generate-multiple-documents.yaml new file mode 100644 index 00000000000..69963f34171 --- /dev/null +++ b/source/includes/steps-test-generate-multiple-documents.yaml @@ -0,0 +1,67 @@ +title: Insert new documents into the ``testData`` collection. +stepnum: 1 +ref: insert-new-documents +pre: | + From the :program:`mongo` shell, use the ``for`` loop. If the ``testData`` + collection does not exist, MongoDB will implicitly create the collection. +action: + language: javascript + code: | + for (var i = 1; i <= 25; i++) { + db.testData.insert( { x : i } ) + } +--- +title: Query the collection. +stepnum: 2 +ref: query-collection +action: + - pre: | + Use :method:`~db.collection.find()` to query the collection: + language: javascript + code: | + db.testData.find() + - pre: | + The :program:`mongo` shell displays the first 20 documents in the + collection. Your :doc:`ObjectId ` values will + be different: + language: javascript + code: | + { "_id" : ObjectId("53d7be30242b692a1138ac7d"), "x" : 1 } + { "_id" : ObjectId("53d7be30242b692a1138ac7e"), "x" : 2 } + { "_id" : ObjectId("53d7be30242b692a1138ac7f"), "x" : 3 } + { "_id" : ObjectId("53d7be30242b692a1138ac80"), "x" : 4 } + { "_id" : ObjectId("53d7be30242b692a1138ac81"), "x" : 5 } + { "_id" : ObjectId("53d7be30242b692a1138ac82"), "x" : 6 } + { "_id" : ObjectId("53d7be30242b692a1138ac83"), "x" : 7 } + { "_id" : ObjectId("53d7be30242b692a1138ac84"), "x" : 8 } + { "_id" : ObjectId("53d7be30242b692a1138ac85"), "x" : 9 } + { "_id" : ObjectId("53d7be30242b692a1138ac86"), "x" : 10 } + { "_id" : ObjectId("53d7be30242b692a1138ac87"), "x" : 11 } + { "_id" : ObjectId("53d7be30242b692a1138ac88"), "x" : 12 } + { "_id" : ObjectId("53d7be30242b692a1138ac89"), "x" : 13 } + { "_id" : ObjectId("53d7be30242b692a1138ac8a"), "x" : 14 } + { "_id" : ObjectId("53d7be30242b692a1138ac8b"), "x" : 15 } + { "_id" : ObjectId("53d7be30242b692a1138ac8c"), "x" : 16 } + { "_id" : ObjectId("53d7be30242b692a1138ac8d"), "x" : 17 } + { "_id" : ObjectId("53d7be30242b692a1138ac8e"), "x" : 18 } + { "_id" : ObjectId("53d7be30242b692a1138ac8f"), "x" : 19 } + { "_id" : ObjectId("53d7be30242b692a1138ac90"), "x" : 20 } + Type "it" for more +--- +title: Iterate through the cursor. +stepnum: 3 +ref: iterate-cursor +pre: | + The :method:`~db.collection.find()` method returns a cursor. To + :doc:`iterate the cursor ` and return more + documents, type ``it`` in the :program:`mongo` shell. The shell will + exhaust the cursor and return these documents: +action: + language: javascript + code: | + { "_id" : ObjectId("53d7be30242b692a1138ac91"), "x" : 21 } + { "_id" : ObjectId("53d7be30242b692a1138ac92"), "x" : 22 } + { "_id" : ObjectId("53d7be30242b692a1138ac93"), "x" : 23 } + { "_id" : ObjectId("53d7be30242b692a1138ac94"), "x" : 24 } + { "_id" : ObjectId("53d7be30242b692a1138ac95"), "x" : 25 } +... diff --git a/source/tutorial/generate-test-data.txt b/source/tutorial/generate-test-data.txt index e6f0652ed51..b4da7f39da9 100644 --- a/source/tutorial/generate-test-data.txt +++ b/source/tutorial/generate-test-data.txt @@ -4,77 +4,19 @@ Generate Test Data .. default-domain:: mongodb -This tutorial describes how to quickly generate test data as you need +This tutorial describes how to quickly generate test data as needed to test basic MongoDB operations. Insert Multiple Documents Using a For Loop ------------------------------------------ -You can add documents to a new or existing collection by using a JavaScript -``for`` loop run from the :program:`mongo` shell. - -1. From the :program:`mongo` shell, insert new documents into the - ``testData`` collection using the following ``for`` loop. If the - ``testData`` collection does not exist, MongoDB creates the - collection implicitly. - - .. code-block:: javascript - - for (var i = 1; i <= 25; i++) db.testData.insert( { x : i } ) - -#. Use find() to query the collection: - - .. code-block:: javascript - - db.testData.find() - - The :program:`mongo` shell displays the first 20 documents in the - collection. Your :doc:`ObjectId ` values will be - different: - - .. code-block:: javascript - - { "_id" : ObjectId("51a7dc7b2cacf40b79990be6"), "x" : 1 } - { "_id" : ObjectId("51a7dc7b2cacf40b79990be7"), "x" : 2 } - { "_id" : ObjectId("51a7dc7b2cacf40b79990be8"), "x" : 3 } - { "_id" : ObjectId("51a7dc7b2cacf40b79990be9"), "x" : 4 } - { "_id" : ObjectId("51a7dc7b2cacf40b79990bea"), "x" : 5 } - { "_id" : ObjectId("51a7dc7b2cacf40b79990beb"), "x" : 6 } - { "_id" : ObjectId("51a7dc7b2cacf40b79990bec"), "x" : 7 } - { "_id" : ObjectId("51a7dc7b2cacf40b79990bed"), "x" : 8 } - { "_id" : ObjectId("51a7dc7b2cacf40b79990bee"), "x" : 9 } - { "_id" : ObjectId("51a7dc7b2cacf40b79990bef"), "x" : 10 } - { "_id" : ObjectId("51a7dc7b2cacf40b79990bf0"), "x" : 11 } - { "_id" : ObjectId("51a7dc7b2cacf40b79990bf1"), "x" : 12 } - { "_id" : ObjectId("51a7dc7b2cacf40b79990bf2"), "x" : 13 } - { "_id" : ObjectId("51a7dc7b2cacf40b79990bf3"), "x" : 14 } - { "_id" : ObjectId("51a7dc7b2cacf40b79990bf4"), "x" : 15 } - { "_id" : ObjectId("51a7dc7b2cacf40b79990bf5"), "x" : 16 } - { "_id" : ObjectId("51a7dc7b2cacf40b79990bf6"), "x" : 17 } - { "_id" : ObjectId("51a7dc7b2cacf40b79990bf7"), "x" : 18 } - { "_id" : ObjectId("51a7dc7b2cacf40b79990bf8"), "x" : 19 } - { "_id" : ObjectId("51a7dc7b2cacf40b79990bf9"), "x" : 20 } - -.. _getting-started-cursor-exhaustion: - -#. The :method:`~db.collection.find()` returns a cursor. To iterate - the cursor and return more documents use the ``it`` operation in - the :program:`mongo` shell. The :program:`mongo` shell will exhaust - the cursor, and return the following documents: - - .. code-block:: javascript - - { "_id" : ObjectId("51a7dce92cacf40b79990bfc"), "x" : 21 } - { "_id" : ObjectId("51a7dce92cacf40b79990bfd"), "x" : 22 } - { "_id" : ObjectId("51a7dce92cacf40b79990bfe"), "x" : 23 } - { "_id" : ObjectId("51a7dce92cacf40b79990bff"), "x" : 24 } - { "_id" : ObjectId("51a7dce92cacf40b79990c00"), "x" : 25 } +.. include:: /includes/steps/test-generate-multiple-documents.rst Insert Multiple Documents with a ``mongo`` Shell Function --------------------------------------------------------- You can create a JavaScript function in your shell session to generate -the above data. The ``insertData()`` JavaScript function, shown here, +the above data. The ``insertData()`` JavaScript function shown here creates new data for use in testing or training by either creating a new collection or appending data to an existing collection: @@ -104,8 +46,9 @@ incremented integer, as in the following example documents: { "_id" : ObjectId("51a4da9b292904caffcff6ed"), "x" : 2 } Store the function in your :ref:`.mongorc.js ` file. -The :program:`mongo` shell loads the function for you every time you -start a session. +The :program:`mongo` shell loads and parses the :ref:`.mongorc.js +` file on startup so your function is available every +time you start a session. .. example:: Specify database name, collection name, and the number of documents to insert as arguments to ``insertData()``. @@ -117,3 +60,10 @@ start a session. This operation inserts 400 documents into the ``testData`` collection in the ``test`` database. If the collection and database do not exist, MongoDB creates them implicitly before inserting documents. + +Additional Resources +-------------------- + +- `MongoDB Cookbook: The Random Pattern `_ + +- `Python utils to create random JSON data and import into mongoDB `_