MongoDB Shell Introduction

Source: Internet
Author: User
Tags findone mongodb mongodb client mongodb server

MongoDB Shell Introduction

MongoDB comes with a JavaScript shell that can interact with the MongoDB real column in the shell using the command line. The shell can perform administrative operations, check for operations such as running a real column, and so on.

One: How do I run the shell?
On the command line, run MONGO to start the shell, as in the following command:

Note: When you start, the shell will automatically connect to the MongoDB server and you must ensure that Mongod is started.

The shell is a full-featured JavaScript parser that can run arbitrary JavaScript programs. Let's take a look at a few simple demos below.

It can also define and invoke JavaScript functions, such as the following code commands:

Two: MongoDB client
After running the command MONGO, the shell connects to the MONGODB server's test database and assigns the database link to the global variable db.
Note: The DB variable is the main entry point for accessing MongoDB through the shell.

If now we want to see the DB currently pointing to that database, you can use the DB command as follows:

The shell also contains some extensions of non-JavaScript syntax, such as selecting a database operation for these ~ code like this:

If you look at the DB variable now, you will see that it is pointing to the Foobar database, such as the following command:

As above, through the DB variable, you can access the collection, for example, through Db.foobar, you can return the Foobar collection of the current database. This means that almost all database operations can be done through the shell.

Three: Shell create, read, update, delete operation.
3.1 Creating an Insert
The Insert function can add a document to the collection, such as the blog's example, we create a local variable called post to represent our document, it will have several keys, respectively, title, content,
and date (release date), as follows:

As above, we use the Insert method to save it to the blog collection, and then we call the collection's Find method to query the data, you can see the data is queried. Let's take a look at the following:

DB is pointing to Foobar, Db.blog is pointing to Foobar.blog;

3.2 Read Find and FindOne
The Find and FindOne methods can be used to query a collection of documents, such as to view a document, you can use the FindOne; As shown below:

3.3 Updating update.
We can update the data using update, the update receives at least two parameters, the first is the qualification (to match the document to be updated). The second one is the new document.
For example, our previous post collection, now want to add a new key, such as the addition of the comments key, for the previous blog added comment function. Then the comment function is definitely an array, because there are multiple data that we can look at as follows:

As above command, first use post.comments = []; Add the Comments property to the Post collection, and then we use Db.blog.find () to query the next blog collection first, and find that there are no comments variables.
Then use Db.blog.update ({title: ' This Is blog '}, post); Update the data, the first parameter is the match to that data, the second parameter is the updated data. Finally, after querying with the Db.blog.find () method, the discovery data is updated.

3.4 Removing remove
Use the Remove method to permanently delete a document from the database, and if you do not use any parameters, just an empty object, such as Db.blog.remove ({}), it will delete all the documents in the collection.
It can also accept a document as a qualification as a parameter, such as Db.blog.remove ({' title ': ' xxx '}); This way, this will delete the title of XXX data;

As shown below:

MongoDB Shell Introduction

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.