concept collection in category mongoDB

appears as: collection, collections, collections, collection, The collection, A collection
Getting MEAN with Mongo, Express, Angular, and Node.js 2ED

This is an excerpt from Manning's book Getting MEAN with Mongo, Express, Angular, and Node.js 2ED.

This less-structured approach means that a collection of documents could have a wide variety of data inside. In the next section, you’ll look at a sample document to get a better idea of what we’re talking about.

  • In MongoDB, a group of documents is called a collection. (Think table if you’re used to relational databases.)
  • > show collections
    Seeing the contents of a collection

    The MongoDB shell also lets you query the collections in a database. The construct for a query or find operation is as follows:

    The query object is used to specify what you’re trying to find in the collection, and you’ll look at examples of this query object later in chapter 6. (Mongoose also uses the query object.) The simplest query is an empty query, which returns all the documents in a collection. Don’t worry if your collection is large, as MongoDB returns a subset of documents that you can page through. Using the startup_log collection as an example, you can run the following command:

    MongoDB in Action, Second Edition: Covers MongoDB version 3.0

    This is an excerpt from Manning's book MongoDB in Action, Second Edition: Covers MongoDB version 3.0.

    Where relational databases have tables, MongoDB has collections. In other words, MySQL (a popular relational database) keeps its data in tables of rows, while MongoDB keeps its data in collections of documents, which you can think of as a group of documents. Collections are an important concept in MongoDB. The data in a collection is stored to disk, and most queries require you to specify which collection you’d like to target.

    In an e-commerce application, you may have multiple collections. Some of these collections may be large, such as a collection storing all user comments, whereas some may be smaller, such as a collection storing all user profiles. In a more complex application such as this, you’d only shard the collections that would benefit from the added capacity of sharding while leaving the smaller collections unsharded for simplicity. Because sharded and unsharded collections can exist in the same system, all of this will work together, completely transparently to the application. In fact, if later you find that one of the collections that didn’t need to be sharded is becoming larger, you can enable sharding at any time.

    As before, you can check the config data to see the change you just made. The config database holds a collection called databases that contains a list of databases. Each document specifies the database’s primary shard location and whether it’s partitioned (whether sharding is enabled):

    > db.getSiblingDB("config").databases.find()
    { "_id" : "admin", "partitioned" : false, "primary" : "config" }
    { "_id" : "cloud-docs", "partitioned" : true, "primary" : "shard-a" }

    Now all you need to do is shard the spreadsheets collection. When you shard a collection, you define a shard key. Here you’ll use the compound shard key {username: 1, _id: 1} because it’s good for distributing data and makes it easy to view and comprehend chunk ranges: