Skip to content

Mongo connect 963 #639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions source/reference/method/Mongo.getDB.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=============
Mongo.getDB()
=============

.. default-domain:: mongodb

.. method:: Mongo.getDB(<database>)

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`
28 changes: 28 additions & 0 deletions source/reference/method/Mongo.txt
Original file line number Diff line number Diff line change
@@ -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 ``<host>`` or
``<host>:<port>``.

- Pass the ``<host>`` parameter to the constructor to
instantiate a connection to the ``<host>`` and the default
port.

- Pass the ``<host>:<port>`` parameter to the constructor to
instantiate a connection to the ``<host>`` and the
``<port>``.

Use the constructor without a parameter to instantiate a
connection to the localhost interface on the default port.

.. seealso:: :method:`Mongo.getDB()`
26 changes: 26 additions & 0 deletions source/reference/method/connect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
=========
connect()
=========

.. default-domain:: mongodb

.. method:: connect( <hostname>:<port>/<database> )

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 ``<hostname>:<port>/<database>``
parameter to connect to the MongoDB instance on the
``<hostname>:<port>`` and return the reference to the database
``<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()`
33 changes: 21 additions & 12 deletions source/tutorial/write-scripts-for-the-mongo-shell.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
<mongo-shell-javascript-file>`, you must assign the ``db`` variable
using the ``getDB()`` method or the ``connect()`` method. You
**cannot** use ``use <dbname>`` 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</core/write-operations>`.
<mongo-shell-javascript-file>`:

- You **cannot** use ``use <dbname>`` 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</core/write-operations>`.

.. _mongo-shell-scripting:

Expand Down