metadata

Database information is stored in collections. They use the system’s namespace:

dbname.system.*
Copy the code

In MongoDB database, namespace <dbname>.system.* is a special Collection containing a variety of system information, as follows:

Collection namespace describe
dbname.system.namespaces List all namespaces.
dbname.system.indexes Lists all indexes.
dbname.system.profile Contains database profile information.
dbname.system.users Lists all users that have access to the database.
dbname.local.sources Contains the information and status of the slave server.

There are restrictions on modifying objects in the system collection.

Insert data in {{system.indexes}} to create indexes. Otherwise, the table information is immutable (the special drop index command automatically updates the information).

{{system.users}} is modifiable. {{system.profile}} is deletable.


MongoDB data type

The following table shows the data types commonly used in MongoDB.

The data type describe
String A string. Data types commonly used to store data. In MongoDB, utF-8 encoded strings are valid.
Integer Integer value. Used to store values. Depending on the server you use, it can be 32-bit or 64-bit.
Boolean Boolean value. Used to store Boolean values (true/false).
Double A double precision floating point value. Used to store floating point values.
Min/Max keys Compare a value to the lowest and highest values of a BSON (binary JSON) element.
Array Use to store an array or list or multiple values as a key.
Timestamp The time stamp. Record the time when the document was modified or added.
Object For inline documents.
Null Used to create null values.
Symbol Symbols. This data type is basically the same as a string type, except that it is generally used in languages that use special symbolic types.
Date Date time. Store the current date or time in UNIX time format. You can specify your own Date and time by creating a Date object and passing in the year, month and day information.
Object ID Object ID. The ID used to create the document.
Binary Data Binary data. Used to store binary data.
Code Code type. Used to store JavaScript code in documents.
Regular expression Regular expression type. Used to store regular expressions.

Several important data types are described below.

ObjectId

ObjectId, like a unique primary key, can be quickly generated and sorted. It contains 12 bytes and has the following meanings:

  • The first four bytes represent the creation of a Unix timestamp, Greenwich Mean TIME (UTC), 8 hours later than Beijing time
  • The next three bytes are the machine id
  • The next two bytes comprise the PID from the process ID
  • The last three bytes are random numbers

Documents stored in MongoDB must have an _ID key. The value of this key can be any type, and the default is an ObjectId object

Because the timestamps created in ObjectId are saved, you do not need to save the timestamp field for your document. You can use the getTimestamp function to get the creation time of the document:

> var newObject = ObjectId()
> newObject.getTimestamp()
ISODate("2017-11-25T07:21:10Z")
Copy the code

ObjectId is converted to a string

> newObject.str
5a1919e63df83ce79df8b38f
Copy the code

string

BSON strings are all utF-8 encoded.

The time stamp

BSON has a special timestamp type for internal MongoDB use that is unrelated to the normal date type. The timestamp value is a 64-bit value. Among them:

  • The first 32 bits are a time_t value (the number of seconds off from the Unix epoch)
  • The last 32 bits are an incremental number of operations in a given secondordinal

Within a single Mongod instance, the timestamp value is usually unique.

In the replication set, Oplog has a TS field. The value in this field represents the operation time using a BSON timestamp.

The BSON timestamp type is primarily used for internal MongoDB use. In most applications, you can use the BSON date type.

The date of

Represents the number of milliseconds from the current Unix epoch (January 1, 1970). Date types are signed, with negative numbers indicating dates prior to 1970.

> var mydate1 = new Date()     //格林尼治时间
> mydate1
ISODate("2018-03-04T14:58:51.233Z")
> typeof mydate1
object
Copy the code
> var mydate2 = ISODate() //格林尼治时间
> mydate2
ISODate("2018-03-04T15:00:45.479Z")
> typeof mydate2
object
Copy the code

The time thus created is of Date type, using the Date type method in JS.

Returns a string of type time:

> var mydate1str = mydate1.toString()
> mydate1str
Sun Mar 04 2018 14:58:51 GMT+0000 (UTC) 
> typeof mydate1str
string
Copy the code

or

> Date()
Sun Mar 04 2018 15:02:59 GMT+0000 (UTC)   
Copy the code

MongoDB create database

grammar

The syntax format for MongoDB to create a database is as follows:

use DATABASE_NAME
Copy the code

If the database does not exist, create the database, otherwise switch to the specified database.

The instance

In the following example, we created the database Runoob:

> use runoob
switched to db runoob
> db
runoob
> 
Copy the code

If you want to see all the databases, use the show DBS command:

> show dbs
admin   0.000GB
local   0.000GB
> 
Copy the code

As you can see, the database we just created, Runoob, is not in the list of databases. To display it, we need to insert some data into the Runoob database.

> db.runoob.insert({"name":" rookie "}) WriteResult({"nInserted" : 1}) > show DBS local 0.078GB runoob 0.078GB test 0.078GB >Copy the code

The default MongoDB database is test. If you do not create a new database, the collection will be stored in test.

Note: In MongoDB, collections are created only after content is inserted! That is, a collection (table) is created before a document (record) is inserted.

MongoDB delete database

grammar

The syntax format for MongoDB to delete a database is as follows:

db.dropDatabase()
Copy the code

Delete the current database, default is test, you can use the db command to view the current database name.

The instance

In the following example, we removed the database Runoob.

First, look at all the databases:

> show dbs
local   0.078GB
runoob  0.078GB
test    0.078GB
Copy the code

Next we switch to the database runoob:

> use runoob
switched to db runoob
> 
Copy the code

Execute delete command:

> db.dropDatabase()
{ "dropped" : "runoob", "ok" : 1 }
Copy the code

Finally, run the show DBS command to check whether the database is deleted successfully:

> show dbs
local  0.078GB
test   0.078GB
> 
Copy the code

Delete the collection

The syntax for collection deletion is as follows:

db.collection.drop()
Copy the code

The following example removes the collection site from the Runoob database:

> use runoob
switched to db runoob
> show tables
site
> db.site.drop()
true
> show tables
> 
Copy the code