Operation MongoDB under CMD

Source: Internet
Author: User
Tags mongodb

MongoDB as a non relational database (NOSQL), it can be composed of the following structure, documents, collections, databases.

The basic unit of MongoDB is the document, which is similar to a row in a relational database, but from the bottom up, the document is more complex than the line. The format of the document is similar to that of the JSON object type in JavaScript, and if you have the base of JSON, MongoDB will not seem too unfamiliar to you. Documents are grouped together by multiple key values in an orderly fashion

Collection resembles a table in a relational database, with multiple documents forming a collection. But unlike a relational database, a key-value pair in a document can be emitted at will, that is, the document in the collection can be set arbitrarily, depending on your needs, but believe that no one will put different data into a collection.

Finally, multiple collections make up the database.

OK, now is not very clear, MongoDB is not as difficult to understand as imagined, if so, developers also develop MongoDB to do.

Here are some examples of CMD operations under MongoDB:

The first step is to ensure that the service is turned on.


D:\mongodb\bin>mongo


The link is now successful, followed by a series of operations on the MongoDB.

View the list of databases

> Show DBS

And if you want to select a database, just like MySQL.

> Use DB

Here's what you need to say, using both the existing database and the new database for another name, for example, if there is no DB database in my MongoDB instance, but with use DB, a new blank database called DB is created.

The DB command allows you to view the DB that is now selected

> DB

> Show Collections
This statement displays a collection of the selected databases.


The following is an action on news, with two additions and lookups

> Db.news.insert ({name: ' 1 '})


> Db.news.find ({name: ' 1 '})


If you look at this, you might think that my document structure is like this, but it's not, and I'm going to take a document out of the original DB collection

> Db.news.findOne ({name:{$ne: ' 1 '}})


This shows that the structure of the set Chinese file can be inconsistent, but inconsistent structure will be your site, your program messy, and eventually you will have to redesign the data structure, if so that mongodb processing capacity is weak, it is really bad.

It says the simplest additions and lookups are now said to be modified as well as deleted.

Delete action:

> Db.news.remove ({_id:objectid ("53edd90a9c937c963d78d0e1")})
This is the deletion of a document that _id equals Objectid ("53edd90a9c937c963d78d0e1") in the news collection, where the _id is like the primary key in the MySQL database, but they are of different types

As you can see, documents _id equal to Objectid ("53edd90a9c937c963d78d0e1") in the news collection have been deleted. This is the same as in MySQL: ' Delete form table name where id = xxx ' effect.

The modification in MongoDB is also easy to use and is implemented via update, which requires at least two parameters, one for the updated constraint and the other for the new document.

I'll add a document first.

> post={' name ': ' Tom ', Age:12}

> Db.news.insert (POST)

> Db.news.find ({age:12}). Limit (1)


Now I'm inserting a new document, but now I need to add a new attribute to this document

> post.address= ' China '

> post.love=[' cartoon ', ' play the game ']

> db.news.update ({name: ' Tom ', Age:12},post)

MongoDB modification can also be implemented by using the $set

For example: I changed Tom's address to America

> db.news.update ({name: ' Tom ', age:12},{$set: {address: ' United States '}})

OK, about MongoDB of the additions and deletions to say so much, of course, MongoDB still have a lot of aggregation usage, this in later article will mention.

Note: Some of the screenshots and the actual data does not match, because the article after the release of many pictures do not show, and then fill the picture, but the principle is not wrong.


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.