|
MongoDB collections are essentially named groupings of documents. You can think of them as roughly equivalent to relational database tables. OverviewA MongoDB collection is a collection of BSON documents. These documents usually have the same structure, but this is not a requirement since MongoDB is a schema-free (or more accurately, "dynamic schema") database. You may store a heterogeneous set of documents within a collection, as you do not need predefine the collection's "columns" or fields. A collection is created when the first document is inserted. Collection names should begin with letters or an underscore and may include numbers; $ is reserved. Collections can be organized in namespaces; these are named groups of collections defined using a dot notation. For example, you could define collections blog.posts and blog.authors, both reside under "blog". Note that this is simply an organizational mechanism for the user -- the collection namespace is flat from the database's perspective. The maximum size of a collection name is 128 characters (including the name of the db and indexes). It is probably best to keep it under 80/90 chars. ShellProgrammatically, we access these collections using the dot notation. For example, using the mongo shell: if( db.blog.posts.findOne() ) print("blog.posts exists and is not empty."); Alternative ways to access collections are: > db["mycol"].find() > db.getCollection("mycol").find() Note that though the underscore character is allowed, it has a special function in the shell if it is the 1st character: the shell considers the property to be an actual javascript value, not a collection name. Consequently it is not accessible using the dot notation, but it works fine with getCollection().
> db._mycol.find() --> error
> db.getCollection("_mycol").find() --> success
See also: |

PLEASE POST QUESTIONS IN THE USER GROUPS FORUM. Post non-question comments and helpful hints here.
blog comments powered by Disqus