C # mongodb [Up],
Overview
MongoDB is a high-performance, open-source, non-pattern document-type database developed using C ++. It is a product between a relational database and a non-relational database. It has the most abundant functions and is most like a relational database. The supported data structure is very loose and is similar to the json bjson format. Therefore, it can store complicated data types. The biggest feature of MongoDB is that it supports a very powerful query language. Its syntax is a bit similar to an Object-Oriented Query Language. It can almost implement most of the functions similar to single-table queries in relational databases, it also supports data indexing. It is a set-oriented, free-of-charge document-type database.
Applicable scenarios
- Websites: MongoDB is ideal for real-time insertion, update, and query. It also supports the replication and high scalability required for real-time website data storage.
- Cache: because of its high performance, MongoDB is also suitable for serving as the cache layer for information infrastructure. After the system is restarted, the persistent cache layer established by MongoDB can avoid data source overload at the lower layer.
- Large-sized and low-value data: using traditional relational databases to store some data may be expensive. Before that, many programmers often choose traditional files for storage.
- High scalability: MongoDB is ideal for databases composed of dozens or hundreds of servers.
- For object and JSON Data Storage: MongoDB's BSON data format is very suitable for storing and querying formatted documents.
Install MongoDB in Windows
1. Download MongoDB on the official website: [http://www.mongodb.org/downloads] select the corresponding operating system. Download Source: zip.
When downloading the MongoDB for Windows version, the official website will prompt: If you are running a version of Windows Server 2008 R2 or Windows 7, install and fix the patch to solve the problem of using the memory ing file on Windows. [Patch]
The installation package includes:
Server-mongod.exe
Client-cmd.exe
Monitoring Tool-maid top.exe
Import and export tool-mongodump.exe, mongorestore.exe, export export.exe, and other tools such as import.exe.
Command to start MongoDB:
When installing MongoDB, you must create a directory to store data files. (Do not include Chinese characters or spaces in the directory ). Cmd: Find the download directory and enter the command mongod -- dbpath C: \ Test \ Data. [The installation method is similar to memcached and Redis]
If the directory contains spaces, double quotation marks (") are used for the entire directory to include: mongod -- dbpath" C: \ Program Files \ mongodb \ data ".
Start Mongodb in the configuration file:
We create a file (Mongodb. config) with the configuration information required by MongoDB in this file. Start mongodb to read the configuration file: config C: \ Test \ mongodb. config
Mongodb. config configuration information:
Dbpath = C: \ Test \ Data
Logpath = C: \ Test \ Log \ log.txt
If any error is reported when reading configuration informationAdministrator identityOpen the cmd command line. Do not include Chinese characters in the config setting path.
Test whether the service is started normally. Enter http: // localhost: 27017/in the browser/
It indicates that the startup is successful.
MongoDB serves as the Window service:
Operate MongoDB as a Window service
Go to the MongoDB client (Shell ). Run cmd to find the installation directory and enter mongo.
Connection to: test automatically connects to the Test database when entering the MongoDB client.
Basic operations on the MongoDB Client
Create a MongoDB database:
Database creation Syntax:
use DATABASE_NAME
The list of newly created databases is empty. To display the database, you need to insert at least one file.
Insert () Basic Syntax:
db.COLLECTION_NAME.insert(document)
Store document data in the "student" collection in the "ck_test_db" Database
MongoDB is the data structure of documents and JSON is basically the same. All data stored in the set is in the BSON format, and BSON is a kind of json Binary storage format, which is short for Binary JSON.
Find () Basic Syntax:
db.COLLECTION_NAME.find()
Db. COLLECTION_NAME.find (). pretty ()
Query condition:
-- MongoDB AND db. COLLECTION_NAME.find ({key1: value1, key2: value2 }). pretty () -- ORdb in MongoDB. COLLECTION_NAME.find ({$ or: [{key1: value1}, {key2: value2}]}). pretty ()
You can use the following operations to query certain conditions of a file:
Operation |
Syntax |
Example |
RDBMS equivalent |
Equality |
{<Key >:< value>} |
Db. mycol. find ({"by": "tutorials point"}). pretty () |
Where by = 'tutorials point' |
Less |
{<Key >:{$ lt: <value> }} |
Db. mycol. find ({"likes": {$ lt: 50}). pretty () |
Where likes <50 |
Less Than Equals |
{<Key >:{$ lte: <value> }} |
Db. mycol. find ({"likes": {$ lte: 50}). pretty () |
Where likes <= 50 |
Greater |
{<Key >:{$ gt: <value> }} |
Db. mycol. find ({"likes": {$ gt: 50}). pretty () |
Where likes> 50 |
Greater Than Equals |
{<Key >:{$ gte: <value> }} |
Db. mycol. find ({"likes": {$ gte: 50}). pretty () |
Where likes> = 50 |
Not Equals |
{<Key >:{$ ne: <value> }} |
Db. mycol. find ({"likes": {$ ne: 50}). pretty () |
Where likes! = 50
|
Basic update () Syntax:
db.COLLECTION_NAME.update( criteria, objNew, upsert, multi )
The update () function accepts the following four parameters:
Criteria: the query condition of the update statement, similar to the where clause after the SQL update statement.
ObjNew: The update object and some updated operators (such as $, $ inc...) can also be understood as
Upsert: this parameter indicates whether to insert objNew if no update record exists. true indicates insertion. The default value is false.
Multi: the default value of mongodb is false. Only the first record found is updated. If this parameter is set to true, all the records identified by the condition are updated.
Delete data:
-- Delete the specified data db. COLLECTION_NAME.remove (DELLETION_CRITTERIA) -- delete all data db. COLLECTION_NAME.remove () -- delete the collection and all data db. COLLECTION_NAME.drop ()
Drop is not displayed.
Here are some basic operations for MongoDB. The next section will share some basic operations for using mongodb in C. You may continue to pay attention to it.
I feel the shared content is helpful to you. Please give me a thumbs up ..