Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

MongoDB Collection management

20 September 2020

Preamble

MongoDB Collection management

In the previous topics, the collection was created implicitly automatically when the first data were added to it. But we can also create it explicitly by using the db.createCollection(name, options) method, where name is the name of the collection and options is an optional object with additional initialization settings. For example:

Clearly creating a collection

db.createCollection("accounts")
{"ok" : 1}

Thus, a collection of accounts is created.

Renaming the collection

You may need to change the name of the collection while working. For example, if there was a misprint in the name of the collection when you first added data. And to avoid deleting and then recreating the collection, you should use the renameCollection function:

db.users.renameCollection("new_name")

And if the renaming goes well, the console will display:

{“ok” : 1}

Limited Edition Collections

When we send a request to the dd for selection, MongoDB returns us the documents in the order in which they were added. However, this order is not always guaranteed because the data can be deleted, moved, or changed.

That’s why MongoDB has a concept of a limited collection (capped collection). Such a collection guarantees that documents will be placed in the same order as they were added to the collection. The limited collections are of a fixed size. And when the collection is no longer in place, the oldest documents are removed and new data are added to the end.

Unlike ordinary collections, we can define limited collections explicitly. For example, we create a limited collection with the name profile and set its size to 9500 bytes:

db.createCollection("profile", {capped:true, size:9500})

And after the collection is successfully created, the console will display it:

{"ok":1}

You can also limit the number of documents in the collection by specifying it in the max parameter:

db.createCollection("profile", {capped:true, size:9500, max: 150})

However, in this way of creating the collection, you should take into account that if all the space for the collection is full (for example, the number of 9500 bytes allocated by us), and the number of documents has not reached the maximum, in this case, 150, then when adding a new document, the oldest document will be removed, and a new document will be inserted in its place.

When updating documents in such collections, one should remember that documents should not grow in size, otherwise the updating will not be possible.

It is also impossible to delete documents from such collections; you can only delete the whole collection.

MongoDB Sub-collections

We can use sub-collections to simplify data organization in collections. For example, the data for a user collection should be divided into profiles and credentials. And we can use db.users.profiles and db.users.accounts to create collections.

In doing so, they will not be in any way related to the user’s collection. That is, there will be three different collections, but in terms of the logical organization of data storage, it may be easier for someone.

Database, Collections, Documents: MongoDB

Enteros

About Enteros

IT organizations routinely spend days and weeks troubleshooting production database performance issues across multitudes of critical business systems. Fast and reliable resolution of database performance problems by Enteros enables businesses to generate and save millions of direct revenue, minimize waste of employees’ productivity, reduce the number of licenses, servers, and cloud resources and maximize the productivity of the application, database, and IT operations teams.

 
Tags: , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...