MongoDB (ii): MongoDB Initial experience: basic use

Source: Internet
Author: User

After the installation of Mongodb , no matter what else, first cool, and then a little bit of detail behind the said.

(1) : Open service : C:\mongodb\bin>mongod--dbpath=c:\mongodb\data

(2): Open client:C:\mongodb\bin>mongo 127.0.0.1:27017

(3): View all databases:Show DBS

(4): using command: use can be used to switch the database, if the switch database does not exist, will create a new database

As above: If the user database exists, it will switch to the user database. If it does not exist, the user database will be created before switching to the user database. But one thing to note: This is the data

Library and Is not really created, only when the data is inserted, the database is actually created, that is, if you create only an empty database without inserting data, then this database is invalid.

After the database is created, basic additions and deletions are started, but before that, it is important to illustrate a small thing: the following

(1) There is no concept of table in MongoDB, which is different from relational database.

(2) MongoDB is mainly composed of three parts, the document, the collection (Collection), and the database.

(3) document is equivalent to a row of records in a database table; Collection corresponds to a table in a relational database and consists of multiple document; Many collection are combined to form the database

The specific relationship between document, collection, and database can be expressed as:

Well, with these preparation knowledge, we began to formally delete and change the operation.

(1): Insert operation:

Description: DB: Indicates the current database;

Persons: Represents a collection for the current database, if it does not exist, is created, if present, with the original;

Insert (): This method is used to insert the document. The inserted data is in Bson format (an extension of JSON)

The above form is represented in the relational database: A table named persons was created, and two records were inserted.

(2): query: Use the Find () method to query out all records. Note: _id is the default for the database to add, the role is equivalent to the primary key in the relational database, to ensure the uniqueness of the data.

(3): Modify: Use the update () method

Description of the modified statement db.persons.update ({"Name": "Zhangsan"}, {$set: {"name": "Wangwu"}}):

(a) In order to understand, it can be understood as such an SQL statement: Update persons set Name= ' Wangwu ' where name= ' Zhangsan ';

(b) The update () method can be interpreted as follows: Update ({Query condition},{update}); (Follow the detailed instructions, first try to say it again)

(4): Delete: Use the Remove () method, such as: Db.persons.remove ({delete condition}), if not write the condition is all delete

Description: Db.persons.remove ({"Name": "Lisi"}): Deleted Name=lisi document, if written as Db.persons.remove () deletes all records

OK, the most basic additions and deletions to explore the experience is finished. Finally, we introduce a few basic commands:

(1):Show Collections : View all the collection in the data;

Where system.indexes is automatically generated by the system

(2): Delete collection: For example Delete Persons collection: Db.persons.drop ();

(3): Delete database: Db.dropdatabase ();

Experience complete

MongoDB (ii): MongoDB Initial experience: basic use

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.