MongoDB installation and that basic operation

Source: Internet
Author: User
Tags bulk insert modifier modifiers

http://www.mongodb.org/

Agile and scalable

MongoDB (from "HuMongoUs") was an Open-source document database, and the leading NoSQL database. Written in C + +, MongoDB features:

  • Document-oriented storage»

    Json-style documents with the dynamic schemas offer simplicity and power.

  • Full Index support»

    Index on any attribute, just like you ' re used to.

  • Replication & High Availability»

    Mirror across LANs and WANs for scale and peace of mind.

  • Auto-sharding»

    Scale horizontally without compromising functionality.

  • Querying»

    Rich, document-based queries.

  • Fast In-place updates»

    Atomic modifiers for contention-free performance.

  • Map/reduce»

    Flexible aggregation and data processing.

  • Gridfs»

    Store files of any size without complicating your stack.

  • MongoDB Management service»

    Monitoring and backup designed for MongoDB.

  • Partner with mongodb»

    Reduce cost, accelerate time-to-market, and mitigate risk with proactive support and enterprise-grade capabilities.

About the benefits of MongoDB, advantages and so on here is not said, the only thing to say is that MongoDB has ternary: database, collection, document , where "collection"

is "table" in the corresponding relational database, "document" corresponds to "row".

One: Download

On the MongoDB official website, we found that there are 32bit and 64bit, this depends on your system, but here are two points to note:

①: According to the industry rules , even "stable version" (such as: 1.6.x,1.8.x), odd for "development version" ( such as: 1.7.x,1.9.x), the difference between the two versions I believe everyone knows it.

②:32bit MongoDB can only store 2G of data, 64bit there is no limit.

①: Before starting, we will assign a folder to MongoDB, which is named "DB", which is used to store mongodb data.

②: Microsoft logo +r, enter CMD, first find the "MongoDB" path, and then run the Mongod Open command, while using--dbpath to specify the data storage location is "DB" folder.

③: Finally to see whether the open success, the information learned that MongoDB uses 27017 port, then we will type "http://localhost:27017/" in the browser,

After opening, MongoDB tells us that on 27017 add 1000 can view MongoDB's management information in HTTP mode.

Because it is the beginning, about the basic "additions and deletions to change", we open a CMD, enter the MONGO command to open the shell, in fact, this shell is the client of MongoDB,

Also is a JS compiler, the default connection is the "test" database .

2. Switch/CREATE Database
>use Yourdb;
The current database is created automatically when a collection (table) is created
Querying all databases
Show DBS;
View the database currently in use
Db.getname ();
db
The DB and GetName methods are the same effect and can query the currently used database
9. Display the current DB status
Db.stats ();

2 Adding Windows System services

Run Cmd.exe

> CD D:\mongodb\bin

> D:\mongodb\bin>mongod--dbpath "D:\mongodb\data\db"--logpath "D:\mongodb\log\mongo.log"--install-- ServiceName "MongoDB"

The service name is MongoDB

Run the command successfully for such as:

The time service has been installed successfully, running

>net Start MongoDB (open service)

>net Stop MongoDB (shutdown service)

3 Closing the database

Shut down by command, and try not to shut down the service directly to prevent the data loss due to incomplete disk writes

>use Admin

>db.shutdownserver ();





<1> Insert Operations

Well, the database has, the next step is the collection, here to take the collection named "Person", note that the document is a JSON extension (Bson) Form.

<2> Find operations

After we insert the data, it is definitely to find out, or plug in the white plug, here to pay attention to two points:

① "_id": This field is the GUID that the database gives us by default, the purpose is to guarantee the uniqueness of the data.

② strictly in accordance with the Bson form of the document, but it doesn't matter, the error is very powerful.

<3> Update operation

The first parameter of the Update method is "find condition", the second parameter is "updated value", and learning C #, I believe it is well understood.

<4> Remove operation

Remove if no parameters will delete all data, oh, very dangerous operation, in MongoDB is a non-revocable operation, think twice before the line.

One: Insert operation

The previous article also said that the document is stored in the "k-v" format, if you are familiar with JSON, I believe that learning MongoDB is extremely easy, we know that the JSON value

It could be "string", maybe "array", and possibly an inline JSON object, and the same way is appropriate for Bson.

Common insert operations also exist in two forms: "Single insert" and "BULK insert".

① Single Insert

As I said earlier, the MONGO command opens a JavaScript shell. So JS syntax in this can work, it seems to be not very bull X.

② BULK Insert

The difference between this thing and the "single insert" I believe you should know that because there is no "bulk Insert method" provided to the shell in MongoDB, it doesn't matter, driver in each language

With MongoDB internal bulk insertion method, because this method is indispensable, if you want to simulate the next batch insert, you can write a for loop, inside is insert.

Two: Find operation

In the daily development, we play the query, play the most is two categories:

①:, >=, <, <=,! =, =.

②:and,or,in,notin

These operations are encapsulated in MongoDB, as described below:

<1> "$gt", "$gte", "$lt", "$lte", "$ne", "No special keywords", which correspond to one by one above, give a few examples.

<2> "No keywords", "$or", "$in", "$nin" I also give a few examples

<3> there is a special match in MongoDB that is "regular expression", which is very powerful.

<4> sometimes query is very complex, very egg pain, but it doesn't matter, MongoDB gave us a big move, it is $where, why say, because $where in the value

Is that we are very familiar with the very love of JS to help us Yimapingchuan.

Three: Update operation

Update operation is nothing more than two, the overall update and local update, the use of the occasion I believe we are also clear.

<1> Overall Update

I do not know you can remember, I in the last article when using the update, in fact, that update is the overall update.

<2> Local Updates

Sometimes we just need to update a field instead of the whole update, so what do we do? Easy question, MongoDB has provided us with two of

Modifiers: $inc and $set.

① $inc modifier

$inc is the abbreviation of increase, the students who have learned SQL Server should be familiar with, for example, we do an online user status record, each modification will be on the original basis

Self-increment $inc The specified value, if the "document" does not have this key, then the key will be created, The following example can be understood.

② $set modifier

Don't say anything, just the code.

<3> Upsert operation (replace into in MySQL same as this)

This is the "word" created by MongoDB, you remember the first parameter of the Update method is "query condition"? , then this upsert operation is saying: if I

Did not find, I was in the database add a, in fact, this also has the benefit, is to avoid me in the database inside the judge is update or add operation, use is very simple

Set the third parameter of update to TRUE.

<4> Batch Update

In MongoDB if you match more than one, the default is to update only the first one, then if we have a need to batch update, then the implementation in MongoDB is also very simple

, set to True in the fourth parameter of the update. The example is not to be lifted.

Today, I would like to share with you the more interesting knowledge of MongoDB, including: aggregation, cursor.

One: Aggregation

Common aggregation operations are the same as SQL Server: Count,distinct,group,mapreduce.

<1> Count

Count is the simplest, easiest, and most commonly used aggregation tool, and its use is exactly the same as the count used in our C #.

<2> DISTINCT

This operation is believed that everyone is also very familiar with, designated who, who can not repeat, direct.

MongoDB installation and that basic operation

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.