This is the 23rd day of my participation in Gwen Challenge

MongoDB data is stored in different collections, which are similar to the tables of relational databases, but the data entity of MongoDB is the whole object stored in the data set. Compared with the table operation of relational database, MongoDB is more convenient.

Creating a data set

There are two ways to create a data set. One is to create a data set automatically if the specified data set does not exist when data is inserted. One is to create data sets explicitly. In the first way, we use the show dataset command to view all existing datasets.

> show collections
author
crud
test
users
Copy the code

Then execute the following insert command to create a new dataset articles:

db.articles.insert({title: 'the article 1', content: Content of the '1'});
Copy the code

Then execute the display data set command, you can see that the Articles data set is added.

> show collections
articles
author
crud
test
users
Copy the code

Another way is to use the command db.createcollection (‘ dataset name ‘) to display the creation of the dataset:

db.createCollection('collection_test');
Copy the code

Capped Collection

A capped collection (fixed size data set) is available in MongoDB, which is capped at a fixed volume and automatically removes old data when storage space is exhausted, enabling cyclic storage. This is very suitable for log storage, you can set a capacity, when too much log storage can automatically remove the old log data. Capped Collection syntax is as follows, using a single parameter configuration:

db.createCollection(
	'capped', 
	{capped: true, size: 512, max: 10}
);
Copy the code

Let’s start by inserting eight pieces of data:

{ "_id" : "1"."name" : Farmers "code 1" }
{ "_id" : "2"."name" : "Code farmer 2" }
{ "_id" : "3"."name" : "Three yards farmers" }
{ "_id" : "4"."name" : Farmers "code 4" }
{ "_id" : "5"."name" : "Code farmers 5" }
{ "_id" : "6"."name" : Farmers "code 6" }
{ "_id" : "Seven"."name" : Farmers "code 7" }
{ "_id" : "8"."name" : "Code farmers 8" }
{ "_id" : "9"."name" : Farmers "code 9" }
{ "_id" : "10"."name" : "Code farmer 10" }
Copy the code

Then continue to insert:

db.capped.insert({name: '11 yards farmers', _id: '11'});
Copy the code

When you look at the data, you will find that the first one has been removed:

{ "_id" : "2"."name" : "Code farmer 2" }
{ "_id" : "3"."name" : "Three yards farmers" }
{ "_id" : "4"."name" : Farmers "code 4" }
{ "_id" : "5"."name" : "Code farmers 5" }
{ "_id" : "6"."name" : Farmers "code 6" }
{ "_id" : "Seven"."name" : Farmers "code 7" }
{ "_id" : "8"."name" : "Code farmers 8" }
{ "_id" : "9"."name" : Farmers "code 9" }
{ "_id" : "10"."name" : "Code farmer 10" }
{ "_id" : "11"."name" : "11 yards farmers" }
Copy the code

This is because the maximum number of rows is limited to 10 at creation time, so rows beyond 10 will be removed from the previously inserted rows. In addition, if our data is too long and exceeds the maximum storage space, old data will also be removed in behavior units.

db.capped.insert({name: 'Isle isle Isle Isle Isle Isle Isle Isle Isle Isle Isle Isle Isle Isle isle isle isle isle isle', _id: '12'});
Copy the code

After repeating the command three times, you will find that two lines are missing the last time, because the total storage space exceeds 512 bytes, causing more lines to be removed. That is, if you specify both the number of rows and the maximum storage space for a fixed size dataset, whichever arrives first will remove the old data. Fixed size datasets do not allow data to be deleted; for example, cannot remove from a capped collection will raise an error.

db.capped.deleteOne({_id: 7});
db.capped.remove({});
Copy the code

However, you can remove the dataset directly using the drop command. To determine if a data set isCapped, use the isCapped method:

> db.capped.isCapped();
true
Copy the code

Display data set

To display the datasets under the database, use the show Collections command.

Delete data set

Drop a data set using the drop method. The format is db.collection.drop().

_id produce

If not specified_id Parameter, MongoDB defaults to generate a 12-byte unique_id._idThe composition is as follows:Since the first 4 bytes are timestamp information, it can be used_idSort through time. Of course, if the display is specified_id, then MongoDB will not generate_id.

conclusion

This article describes the basic operations of a MongoDB dataset, focusing on the use of a capped Collection fixed size dataset. In practice, you can use capped Collection to save space by storing data that becomes unimportant over time, such as log data.