diff --git a/source/reference/method/Mongo.getDB.txt b/source/reference/method/Mongo.getDB.txt new file mode 100644 index 00000000000..caf42eb7d5e --- /dev/null +++ b/source/reference/method/Mongo.getDB.txt @@ -0,0 +1,22 @@ +============= +Mongo.getDB() +============= + +.. default-domain:: mongodb + +.. method:: Mongo.getDB() + + Use the :method:`Mongo.getDB()` method to access the database from + the :program:`mongo` shell or from a JavaScript file. + + :param string database: The name of the database to access. + + The following example instantiates a new connection to the MongoDB + instance running on the localhost interface and returns a reference + to ``"myDatabase"``: + + .. code-block:: javascript + + db = new Mongo().getDB("myDatabase"); + + .. seealso:: :method:`Mongo()` and :doc:`/reference/method/connect` diff --git a/source/reference/method/Mongo.txt b/source/reference/method/Mongo.txt new file mode 100644 index 00000000000..b37f9a8137d --- /dev/null +++ b/source/reference/method/Mongo.txt @@ -0,0 +1,28 @@ +======= +Mongo() +======= + +.. default-domain:: mongodb + +.. method:: Mongo() + + JavaScript constructor to instantiate a database connection from the + :program:`mongo` shell or from a JavaScript file. + + :param string host: + + Optional. Either in the form of ```` or + ``:``. + + - Pass the ```` parameter to the constructor to + instantiate a connection to the ```` and the default + port. + + - Pass the ``:`` parameter to the constructor to + instantiate a connection to the ```` and the + ````. + + Use the constructor without a parameter to instantiate a + connection to the localhost interface on the default port. + + .. seealso:: :method:`Mongo.getDB()` diff --git a/source/reference/method/connect.txt b/source/reference/method/connect.txt new file mode 100644 index 00000000000..c37f0280cb1 --- /dev/null +++ b/source/reference/method/connect.txt @@ -0,0 +1,26 @@ +========= +connect() +========= + +.. default-domain:: mongodb + +.. method:: connect( :/ ) + + The :method:`connect()` method creates a connection to a MongoDB instance. + However, use the :method:`Mongo()` object and its + :method:`~Mongo.getDB()` method in most cases. + + :method:`connect()` accepts a string ``:/`` + parameter to connect to the MongoDB instance on the + ``:`` and return the reference to the database + ````. + + The following example instantiates a new connection to the MongoDB + instance running on the localhost interface and returns a reference + to ``myDatabase``: + + .. code-block:: javascript + + db = connect("localhost:27017/myDatabase") + + .. seealso:: :method:`Mongo.getDB()` diff --git a/source/tutorial/write-scripts-for-the-mongo-shell.txt b/source/tutorial/write-scripts-for-the-mongo-shell.txt index 6611f6e7625..558af32048b 100644 --- a/source/tutorial/write-scripts-for-the-mongo-shell.txt +++ b/source/tutorial/write-scripts-for-the-mongo-shell.txt @@ -20,7 +20,8 @@ Opening New Connections ----------------------- From the :program:`mongo` shell or from a JavaScript file, you can -instantiate database connections using the ``Mongo()`` constructor: +instantiate database connections using the :method:`Mongo()` +constructor: .. code-block:: javascript @@ -30,29 +31,37 @@ instantiate database connections using the ``Mongo()`` constructor: Consider the following example that instantiates a new connection to the MongoDB instance running on localhost on the default port and sets -the current ``db`` to ``myDatabase`` using the ``getDB()`` method: +the global ``db`` variable to ``myDatabase`` using the +:method:`~Mongo.getDB()` method: .. code-block:: javascript conn = new Mongo(); db = conn.getDB("myDatabase"); -Additionally, you can use the ``connect()`` method to connect to the -MongoDB instance. The following example connects to the MongoDB -instance that is running on ``localhost`` with the non-default port -``207020`` and set the current ``db``: +Additionally, you can use the :method:`connect()` method +to connect to the MongoDB instance. The following example connects to +the MongoDB instance that is running on ``localhost`` with the +non-default port ``27020`` and set the global ``db`` variable: .. code-block:: javascript db = connect("localhost:27020/myDatabase"); If you create new connections inside a :ref:`JavaScript file -`, you must assign the ``db`` variable -using the ``getDB()`` method or the ``connect()`` method. You -**cannot** use ``use `` inside the file. Additionally, inside -the script, you would need to call :method:`db.getLastErrorObj()` or -:method:`db.getLastError()` explicitly to wait for the result of -:doc:`write operations`. +`: + +- You **cannot** use ``use `` inside the file to set the ``db`` + global variable. + +- To set the ``db`` global variable, use the :method:`~Mongo.getDB()` + method or the :method:`connect()` method. You can, of + course, assign the database reference to a variable other than ``db``. + +- Additionally, inside the script, you would need to call + :method:`db.getLastErrorObj()` or :method:`db.getLastError()` + explicitly to wait for the result of :doc:`write + operations`. .. _mongo-shell-scripting: