MongoDB Learning to use

Source: Internet
Author: User
Tags failover mongodb mongodb connection string mongodb port number

First, what is MongoDB?

MongoDB is a high-performance, open-source, modeless document-based database that is a popular one in the current NoSQL database. It can be used in many scenarios to replace the traditional relational database or key/value storage methods .

NOSQL, the full name is not-only Sql, refers to the non-relational database. The next generation of databases mainly solves several key points: non-relational, distributed, open-source, horizontally extensible. The original goal was for large-scale Web applications, which began in early 2009 and typically features

Applications such as: freedom of mode, support for simple replication, simple APIs, final consistency (non-acid), high-capacity data, etc. NoSQL is the most used when the number of key-value storage, of course, there are other document-type, column storage, graph database, XML database and so on.

  

Characteristics:

High performance, easy to deploy, easy to use, easy to store data. The main features are:

For collection storage, easy to store data for object types.
Mode of freedom.
Supports dynamic queries.
Supports full indexes, including internal objects.
Support Queries.
Supports replication and recovery.
Use efficient binary data storage, including large objects such as video.
Automatically process fragmentation to support scalability at the cloud level
Drivers for Python,php,ruby,java,c,c#,javascript,perl and C + + languages are supported, and drivers for Erlang and. NET platforms are also available in the community.
The file storage format is Bson (an extension of JSON).
can be accessed over the network.


Function:

Collection-oriented storage: suitable for storing objects and data in JSON form.
Dynamic query: MONGO supports rich query expressions. Query directives use a JSON-style tag to easily query objects and arrays embedded in the document.
Full index support: Includes embedded objects and arrays in the document. The query optimizer of MONGO parses the query expression and generates an efficient query plan.
Query monitoring: MONGO contains a monitoring tool to analyze the performance of database operations.
Replication and automatic failover: The MONGO database supports data replication between servers, supporting master-slave mode and inter-server replication. The primary goal of replication is to provide redundancy and automatic failover.
Efficient traditional storage: supports binary data and large objects (such as photos or pictures)
Auto-sharding to support cloud-scale scalability: Automatic sharding supports a level of database clustering, adding additional machines dynamically.


Applicable occasions:

Website data: MONGO is ideal for real-time inserts, updates and queries, as well as the replication and high scalability required for real-time data storage on the site.
Caching: Because of its high performance, MONGO is also suitable as a caching layer for the information infrastructure. After the system restarts, the persistent cache layer built by MONGO can avoid overloading the underlying data sources.
Large, low-value data: Storing some data in a traditional relational database can be expensive, and many times programmers often choose traditional files for storage.
Highly scalable scenario: The MONGO is ideal for databases made up of dozens of or hundreds of servers. Built-in support for the MapReduce engine is already included in the roadmap for MONGO.
Storage for objects and JSON data: MONGO's Bson data format is ideal for storing and querying in document formats

First, the installation of MongoDB use

1, download MongoDB, create a new folder named MongoDB, unzip it into the inside

Create data under MongoDB and create db:d:\mongodb\data\db under Data

Because you need to create a storage folder for the database files before you start the MongoDB service, the command is not created automatically and does not start successfully.

2. Start Mongdb

4, in the browser input http://localhost:27017 (27017 is MongoDB port number) view, if shown:

Connection Successful

5. Configure WINDWOS Service

CMD jumps to the D:\\mongodb\bin directory first.

Input: Mongod--config "D:\Mongodb\mongo.config"--install--servicename "Mongodb"

That is, based on the Mongo.config profile installation service you just created, the name is MongoDB.

Ii. using MONGDB in C # projects

1. Download the DLL for C # provided by MongoDB and introduce the project

2. Create an entity that inherits an entity, which is similar to a GUID in C #, and the type is Onjectid, which must be used to store data in MongoDB.

Mongoentity

3. MongoDB connection string in project configuration file

4, the main program connected to the MongoDB database, write query data

1 class Program2     {3         Static voidMain (string[] args)4         {5             stringConnStr = configurationmanager.appsettings["mongoserversettings"];//Get connection string6 7Mongoserver Server = Mongoserver.create (CONNSTR);//create an object for the MONGDB service8 9Mongodatabase db = server. Getdatabase ("MyDB");//gets the database, and if not, it automatically creates aTen  One             varCollectionName =typeof(MyModel). Name;//Specify the name of the collection A  -             varCollection = db. Getcollection<mymodel> (CollectionName);//gets the collection, if the collection does not exist, creates a direct -  the  -             //Add entity -              for(inti =0; I < -; i++) -             { +MyModel model =NewMyModel (); -Model. Id =i; +Model. Name ="Jenkin"+i; AModel. Subtime =DateTime.Now; atCollection. Insert (model);//inserting a data source into the collection -  -             } -  -Console.WriteLine (collection. Count ());//How many data are printed -  in  -             //Delete name Jenkin or name 123. toCollection. Remove (Query.or (Query.eq ("Name","Jenkin"), Query.eq ("Name","123"))); +  -  the  *             //Enquiry $             //1, according to the ID query aPanax Notoginseng             varQuerydaat = collection. Findonebyid (1); -  the             //Find by Condition +             varQueryData2 = collection. Find (Query.eq ("Name","Jenkin")); A             foreach(varModelinchqueryData2) the             { + Console.WriteLine (model. Name); -             } $  $         } -}

  

MongoDB Learning to 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.