Skip to content
Merged
Changes from 1 commit
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
88 changes: 88 additions & 0 deletions draft/core/collections.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
===========
Collections
===========

.. default-domain:: mongodb

MongoDB :term:`collections` are named groupings of documents. You can
think of them as roughly equivalent to relational database tables.

A MongoDB collection is a collection of :term:`BSON` :term:`documents
<document>`. MongoDB is a schema-free database and does not predefine a
collection's fields. The documents within a collection may or may not
have the same structure. Any new document can add new fields to a
collection.

Collections can be created explicitly using the
:method:`db.createCollection()` method but can also be created
implicitly by specifying a new collection name when creating a document
using the :method:`insert() <db.collection.insert()>` method.

Collection Names
----------------

A collection name can be any UTF-8 string that follows these standards:

- Collection names should begin with a letter or an underscore (_).

- Collection names can include numbers.

- Collection names *cannot* include the ``$`` character, which is a
reserved character.

- Collection names *cannot* include the null character.

- Collection names *cannot* be an empty string (e.g. ``""``).

- Collection names *cannot* begin with the ``system.`` prefix, which is
reserved for system collections.

- The maximum size of a collection name is 128 characters, including the
name of the database. However, for maximum flexibility, collections
should have names less than 80 characters.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 24-42 are redundant to: http://docs.mongodb.org/manual/reference/limits/#Restriction%20on%20Collection%20Names (if there are oversights in that section we should edit that...)


Collections can be organized by namespaces. Namespaces are named groups
of collections defined using a dot notation. For example, the
collections ``blog.posts`` and ``blog.authors`` are in the ``blog``
namespace.

Namespaces are an organizational method for the database user and do not
imply an underlying organizational structure in the database itself.
From database perspective, there is no relationship between collections
in the same namespace.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't overload the term "namespace" which refers to the concatenation of the database name and collection name, typically. The fact that you can put a dot in collection names and get something that looks like hierarchical namespaces, is a hack (a decent one, but still a hack,) and it gives the wrong impression about how collections works.

Better to say, "Collection names can have a dot, and you may use this to simulate a hierarchical namespace; however, the collection namespace for each database is itself flat." and leave it at that.

Perhaps you can contrive this into an FAQ and use the example in the answer.


View a List of Collections
--------------------------

From the :program:`mongo` shell, you can view list of collections in a
database using the method:`getCollectionNames()
<db.getCollectionNames()>` method.

Query a Collection
------------------

From the :program:`mongo` shell, you can query a collection's documents
using the MongoDB :method:`find() <db.collection.find()>` and
:method:`findOne() <db.collection.findOne()>` methods. For more
information, see :doc:`/applications/read`.

If your collection name begins with an underscore, then to query the
collection's documents from the :program:`mongo` shell you must use the
:method:`getCollection() <db.getCollection()>` method along with the
query operator (e.g. :method:`find() <db.collection.find()>`).

Though underscores are allowed at the beginnings of collection names,
the :program:`mongo` shell considers the underscore character to be a
javascript value, not a collection name. The :method:`getCollection()
<db.getCollection()>` method gets around this. Consider the following example:

.. code-block:: javascript

db.getCollection( "_mycollection" ).find()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

entirely redundant to the read document. if db.getCollection() isn't fully documented, or is lacking somewhere, some of these details can go in that document.


Capped Collections
------------------

MongoDB includes a special type of collection, called a :term:`capped
collection`, that is used for certain high-throughput operations, such
as journaling. See :doc:`/core/capped-collections`.