Reprint C # MongoDB
Overview
MongoDB is a high-performance, open-source, modeless, document-based database that is developed using C + +. is a product between a relational database and a non-relational database, the most versatile of the non-relational databases, most like relational databases. The data structure he supports is very loose and is a JSON-like Bjson format, so you can store more complex data types. The biggest feature of MongoDB is that the query language he supports is very powerful, its syntax is a bit like object-oriented query language, almost can realize the most functions like relational database single table query, but also support the index of data. It is a set-oriented, schema-free, document-based database.
Applicable scenarios
- Website: MongoDB is ideal for real-time insertions, 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, MongoDB is also suitable as a caching layer for the information infrastructure. After the system restarts, the persistent cache layer built by MongoDB avoids overloading the underlying data sources.
- Large, low-value data: Storing some data in a traditional relational database can be expensive, before many programmers often choose traditional files for storage.
- Highly scalable scenario: MongoDB is ideal for databases consisting of dozens of or hundreds of servers.
- Storage for objects and JSON data: MongoDB's Bson data format is ideal for storing and querying document formats.
Windows installation MongoDB
1. Download MongoDB on the official website: "Http://www.mongodb.org/downloads" select the corresponding operating system. Download Source: Zip.
When you download the MongoDB for Windows version, you will be prompted: If the version you are running is Windows Server R2 or Windows 7, install the fix patch to resolve the issue of using memory mapped files on Windows. Patch
The installation package includes:
Server-mongod.exe
Client-mongo.exe
Monitoring Tools-Mongostat.exe,mongotop.exe
Import and Export tools-mongodump.exe, Mongorestore.exe,mongoexport.exe,mongoimport.exe and other tools.
command to start MongoDB:
When you install MongoDB, you need to create a directory to hold the data files. (Do not take Chinese or spaces in the directory.) cmd, find the download directory, enter the command: Mongod--dbpath C:\Test\Data "installation method is similar to Memcached,redis"
If there are spaces in the directory, enclose the entire directory in double quotation marks: Mongod--dbpath "C:\Program files\mongodb\data".
configuration file Start MongoDB:
We create a file (mongodb.config) of the configuration information required by MongoDB in this file. Then start MongoDB Read profile information: Config C:\Test\mongodb.config
Mongodb.config configuration information:
Dbpath=c:\test\data
Logpath=c:\test\log\log.txt
Execute Read configuration information if there is an error, open the cmd command line as an administrator . Do not include Chinese in the Config settings path.
Test whether the service starts properly. In the browser, type: http://localhost:27017/
Proof of successful start-up.
MongoDB as Window service:
Make MongoDB a window service operation
Enter the MongoDB client (Shell). CMD find the installation directory input: MONGO
Connection to:test into the MongoDB client automatically connects us to the test database.
MongoDB Client Basic operations
MONGODB CREATE DATABASE:
Database creation Syntax:
Use database_name
The newly created database list is empty. To display a database, you need to insert it into 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 document's data structure and JSON basically the same. All data stored in the collection is in Bson format, and Bson is a binary form of a class JSON storage format, referred to as binary JSON.
Find () Basic syntax:
Db. Collection_name.find ()
The Find () method displays all the files in an unstructured manner.
Pretty () Method:
Db. Collection_name.find (). Pretty ()
Query criteria:
--mongodb in and DB. Collection_name.find ({key1:value1, key2:value2}). Pretty ()--mongodb in ORdb.COLLECTION_NAME.find ( { $or: [ { Key1:value1}, {key2:value2} ] }). Pretty ()
To query the file based on some conditions, you can use the following actions
Operation |
Grammar |
Example |
RDBMS equivalent |
Equality |
{<key>:<value>} |
Db.mycol.find ({"By": "Tutorials Point"}). Pretty () |
where by = ' tutorials point ' |
Less Than |
{<key>:{$lt: <value>}} |
Db.mycol.find ({"likes": {$lt:). Pretty () |
Where likes < 50 |
Less Than Equals |
{<key>:{$lte: <value>}} |
Db.mycol.find ({"likes": {$lte:). Pretty () |
where likes <= 50 |
Greater Than |
{<key>:{$gt: <value>}} |
Db.mycol.find ({"likes": {$gt:). Pretty () |
where likes > 50 |
Greater Than Equals |
{<key>:{$gte: <value>}} |
Db.mycol.find ({"likes": {$gte:). Pretty () |
where likes >= 50 |
Not Equals |
{<key>:{$ne: <value>}} |
Db.mycol.find ({"likes": {$ne:). Pretty () |
where likes! = 50
|
Update () Basic syntax:
Db. Collection_name.update (criteria, objnew, Upsert, Multi)
The update () function accepts the following four parameters:
The criteria:update query condition, similar to that in SQL Update query, is behind.
Objnew:update objects and some updated operators (such as $, $inc ... ) can also be understood as the SQL update query within the set after the
Upsert: This parameter means that if there is no record of update, insert Objnew,true is inserted, default is False, not inserted.
Multi:mongodb default is False, only update the first record found, if this parameter is true, the condition is checked out all the records are updated.
Delete data:
--Deletes the specified data db. Collection_name.remove (Delletion_critteria)--Delete all data db. Collection_name.remove ()--Deletes all data db in the collection and collection. Collection_name.drop ()
Drop is not present.
Some basic operations of MongoDB This chapter is shared here, and the next section will share some of the basic operations of using MongoDB in C #. Intentionally can continue to pay attention to OH.
C # mongodb 1