MongoDB Database Basic Operations

Source: Internet
Author: User

I. Use of the database

Show all database commands

$ . /  3.0. 6  to : Test>  show dbslocal  0. 078GBtest    0. 078GB>

Displays the current database object

$ . /  3.0. 6  to: Test>  dbtest>

Switch database

>  Use   to db local>  dblocal>

Connecting to a database

From the above we already know that the connection database uses the "MONGO" command, which is connected to the test database by default, and of course the command can take parameters such as

$ . /MONGO localhost:27017

Represents a connection to the local database service, localhost represents the hostname, and here is the default connection to the test database

> dbtest >

Note the following port number must be added otherwise, connect to the localhost database, even if localhost is empty

$ . /  3.0. 6  to: localhost>  dblocalhost>show dbslocal   0. 078Gtest   0. 078G
>

If you are connecting to a MONGODB service in another programming language, you need to provide a connection string that is commonly written as

MongoDB://username:password@hostname/dbname
More connection examples (this section refers to the MongoDB rookie tutorial)

Connect to the local database server, the port is the default.

MongoDB://localhost

Use username fred, password foobar login to localhost's admin database.

MongoDB://fred:foobar@localhost

Use user name Fred, password foobar log on to localhost's Baz database.

MongoDB://fred:foobar@localhost/Baz

Connect replica pair, server 1 is example1.com server 2 is example2.

MongoDB://example1.com:27017, example2.com:27017

Connect replica set three servers (ports 27017, 27018, and 27019):

MongoDB://localhost,localhost:27018, localhost:27019

Connect replica set three servers, write operations to the primary server, and distribute the query to the slave server.

MongoDB://host1,host2,host3/? slaveok=true

Connect directly to the first server, either replica set part or master server or slave server.

MongoDB://host1,host2,host3/? Connect=Direct;slaveok=true

When your connection server has a priority, you also need to list all the servers you can use to connect.

Safe Mode connection to localhost:

MongoDB://localhost/? safe=true

Connect to replica set in Safe mode, and wait for at least two replication servers to write successfully, and the time-out is set to 2 seconds.

MongoDB://host1,host2,host3/Safe=true;w=2; Wtimeoutms=  -
parameter Option Description

Standard format:

MongoDB://[username:[email protected]]host1[:p ort1] [  , host2[:p ort2],... [, hostn[:p ortn]] [/[database] [options]]

The standard connection format contains several options, as follows:

Options Description
Replicaset=name Verify the name of the replica set. Impliesconnect=replicaset.
Slaveok=true|false
  • true: In Connect=direct mode, the driver will connect to the first machine, even if the server is not the primary one. In Connect=replicaset mode, the driver sends all write requests to the master and distributes the read operations to the other slave servers.
  • False: In Connect=direct mode, the driver will automatically find the primary server. In Connect=replicaset mode, the driver only connects to the primary server, and all read and write commands are connected to the primary server.
Safe=true|false
  • True: After the update operation, the driver sends the GetLastError command to ensure that the update succeeds. (also refer to WTIMEOUTMS).
False: After each update, the driver does not send GetLastError to ensure that the update succeeds.
W=n The driver adds {w:n} to the GetLastError command. Apply to Safe=true.
Wtimeoutms=ms The driver adds {wtimeout:ms} to the GetLastError command. Apply to Safe=true.
Fsync=true|false
  • True: The driver adds {fsync:true} to the GetLastError command. Applies to safe=true.
  • False: The driver is not added to the GetLastError command.
Journal=true|false If set to True, synchronizes to Journal (written to the entity before committing to the database). Apply to Safe=true
Connecttimeoutms=ms The time at which the connection can be opened.
Sockettimeoutms=ms Time to send and receive sockets.

Second, create a database

Syntax format

 use DatabaseName

If the database does not exist, create the database, or switch to the specified database. Note the newly created database is empty and does not appear in the results of the show DBS statement if no records are inserted into it

Third, delete the database

Syntax format

Db.dropdatabase ()

Delete the current database, which is test by default, and you can use the DB command to view the current database name.

Note After you delete the database, the database referenced by DB is still a deleted database, except that the database is now empty

MongoDB Database Basic Operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.