Simple use of mongodb (startup, command, data type)

Simple use of mongodb

Let's learn about the simple use of mongdb

1. Start of mongodb server
Default port: 27017 The location of the default configuration file: /etc/mongod.conf The location of the default log: /var/log/mongodb/mongod.log

There are two ways to start the mongodb server:
a. Start the local test mode (only with the function of adding, deleting, modifying and checking local data)
b. Starting the production environment (with complete all functions)

1.1 Start in test mode Start
: sudo service mongodb start (sudo service mongod start)
Stop: sudo service mongodb stop
Restart: sudo service mongodb restart
1.2 The official start method of the production environment
Start:

sudo mongod [--auth --dbpath=dbpath --logpath=logpath --append --fork] [-–f logfile ]

When only started with the sudo mongod command, the data is stored in the /data/db directory by default and needs to be created manually
–dbpath: Specify the storage path of the database
–logpath: Specify the storage path of the log
–append: or –logappend Set the log writing The
input form is the append mode --fork: or -fork to start a new process to run the mongodb service
--f: or -f configuration file path (you can write the above configuration information into a file and then load and start it through the parameters in the file)
--auth: Start with permission authentication

1.3 Check whether the startup is successful, check the process
ps aux | grep mongod from the terminal command line

2. Start the mongodb client:
Enter the mongo shell to
start the local client: mongo
view help: mongo -help
exit: exit or ctrl+c

3. Simple use of mongodb
When the mongodb server is turned on, you can simply use it after entering the mongo shell

3.1 Mongodb database commands
View the current database: db (the test database is used by default if the database is not switched)
View all databases: show dbs /show databases
Switch the database: use db_name
db_name is the database name returned after show dbs
Delete the current database Database: db.dropDatabase()

3.2 Commands for mongodb collections
No need to create collections manually: the first time you add data to a non-existent collection, the collection will be created automatically.
Create collections manually:
db.createCollection(name,options)
db.createCollection("stu")
db .createCollection("sub", {capped: true, size: 10})
Parameter capped: The default value is false means no upper limit is set, and a value of true means upper limit is set.
Parameter size: the number of bytes occupied by the collection. When the capped value is true, you need to specify this parameter, which indicates the upper limit size. When the document reaches the upper limit, the previous data will be overwritten, in bytes.
View collection: show collections
delete collection: db.collection name.drop()
check Whether the collection has an upper limit: db.collection name.isCapped()

3.3 Common data types in mongodb
3.3.1 Common types
Object ID: document ID/data ID, primary key of data
String: string, most commonly used, must be valid UTF-8
Boolean: store a Boolean value, true or false
Integer: Integer can be 32-bit or 64-bit, depending on the server
Double: Floating-point number
Arrays: Array/list
Object: A piece of data/document in mongodb, namely document nested document
Null: Store null value
Timestamp: Time stamp The total number of seconds from 1970-1-1 to the present
Date: The UNIX time format for storing the current date or time

3.3.2 Points to Note
Each document has an attribute, _id, to ensure the uniqueness of each document, mongodb uses _id as the primary key by default

You can manually set the value of _id. If it is not provided, then MongoDB provides a unique _id for each document. The type is objectID. The
objectID is a 12-byte hexadecimal number, with two digits per byte, totaling Is a 24-bit string:

The first 4 bytes are the current timestamp, the
next 3 bytes of the machine ID, the
next 2 bytes, the
last 3 bytes of the MongoDB service process id is a simple increment value

Guess you like

Origin blog.csdn.net/li944254211/article/details/109255075