MongoDB Learning Notes

Source: Internet
Author: User

1. Login to MongoDB

The following command is used to log in to the MongoDB database. Make sure that the dbname corresponding user's credentials (for example, user name and password) exist in the database.

Mongo-u <username>-P <password>--authenticationdatabase <dbname>
2. Show all databases

Once the user has logged in to the database in the appropriate role, such as Useradmin or useradminanydatabase, you can view all the databases with this command:

Show DBS
3. Select a working database

To use the specified database, execute the following command:

Use DatabaseName
4. Authentication and Logout Database

When you use a command that contains dbname to switch to another database, you need to authenticate with a valid database user. You can use the following command to authenticate:

Authentication//db.auth ("username", "password");////logout//db.logout ()
5. List collections, users, roles, and more

The following commands can be used to check for existing collections, users, roles, and so on

Lists all the collections in the current database//show collections;db.getcollectionnames ();////lists all the users in the current database//show users;db.getusers ();//// List all the characters//show roles
6. Create a Collection

The following command can be used to create a collection. For detailed instructions on how to use this command, refer to here.

Db.createcollection ("CollectionName");
7. Inserting a document into a collection

Once the collection is created, the next step is to insert the document into it. Here is a simple example of inserting a document into a collection.

Insert a simple document//db.<collectionname>.insert ({field1: "value", Field2: "Value"})////Insert multiple documents//db.< Collectionname>.insert ([{field1: "value1"}, {field1: "value2"}]) Db.<collectionname>.insertmany ([{field1: "Value1"}, {field1: "value2"}])
8. Save or update the document

The Save command performs an update of an existing document or inserts a new document, depending on the parameters passed in. If the _id matches an existing document, the document is updated. Otherwise, a new document is created. Inside the system, the Save method either uses the Insert command or uses the update command.

If the document is matched, the update is inserted, and if no document for the corresponding ID is found, insert the new document//db.<collectionname>.save ({"_id": New ObjectId ("JHGSDJHGDSF"), Field1: "Value", Field2: "Value"});
9. Display the records in the collection

The following command can be used to display all records in the collection:

Get All Records//db.<collectionname>.find ();////gets the specified number of records; The following command displays 10 Records//db.<collectionname>.find (). Limit (10);////Gets the record//db.<collectionname>.find ({"_id": ObjectId ("Someid")}) based on the ID, and////gets the specified recordset//db based on the passed-in property value. <collectionname>.find ({"_id": ObjectId ("Someid")}, {field1:1, field2:1});d B.<collectionname>.find ({"_ ID ": ObjectId (" Someid ")}, {field1:0}); Exclude field1////The number of records in the collection//db.<collectionname>.count ();
10. Management commands

Here are some administrative commands that you can use to view the details of a collection, such as storage size, total size, overall statistics, and so on.

Gets the statistics of collection Statistics//db.<collectionname>.stats () db.printcollectionstats ()////Read and write operations delay, including average read, write, and time//db for some other operations. <collectionname>.latencystats ()////Get collection size for data and indexes//db.<collectionname>. DataSize ()//Collection size db.<collectionname>.storagesize ()//collection file total storage size db.<collectionname>.totalsize ()// The size of the collection and its indexes (bytes) db.<collectionname>.totalindexsize ()//the size of all indexes in the collection


MongoDB Learning Notes

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.