MongoDB usage Summary (C ),

Source: Internet
Author: User

MongoDB usage Summary (C ),
Introduction

MongoDB is a non-relational and document-type database, which facilitates direct access to the class type ...... The official website provides Free courses on a regular basis. MongoDB is used in the previous project. My projects now use EF, so I want to sum up the methods, precautions, and ideas used by MongoDB. After all, if I use them again, it will be a waste of time.

DLL

(1) MongoDB. Bson. dll
(2) MongoDB. Driver. dll

Software

Good visualization software, dedicated vue

Method connection
// Mongo database name private relational database Mg_db; // Mongo database collection name public relational collection Mg_col; // your serverprivate relational server; // connection string (beginning with mongodb) string connectionString = "mongodb: // localhost"; // or 192. xxx. xxx. xxx // string constr = "mongodb: //" + ip + ":" + port; // connect the ip address and the port // connect to the Mongo database var client = new MongoClient (connectionString); // obtain a client that references mongoServer = client. getServer ();
Create a database
Mg_db = internal server. GetDatabase (dbName); // dbName indicates the name of the new database. // If dbName already exists, Mg_db indicates the get database. Otherwise, it is created.
Create a table (SET)
Mg_col = Mg_db.GetCollection <T> (connectionName); // connectionName indicates the name of the new table. // If the connectionName table already exists in the Database, Mg_col indicates the get table. Otherwise, it is created.
Insert data into a table
Public void Insert <T> (string dbName, string connectionName, T t) {Mg_db = mongoServer. getDatabase (dbName); // get a database named collections collection = Mg_db.GetCollection <T> (connectionName); // get a table collection in this database. insert (t); // Insert (any type )}
Delete data from a table
Public void delete <T> (string dbName, string connectionName, string key, BsonValue value) {Mg_db = mongoServer. getDatabase (dbName); Mg_col = Mg_db.GetCollection <T> (connectionName); var query = Query. EQ (key, value); // query condition, eg: I want to delete the data that name is Zhang San, key, field name, value is Zhang San Mg_col.Remove (query );}
Update a data entry in a table
Public void UpData (string dbName, string connectionName, string key, BsonValue value, People t) {Mg_db = mongoServer. getDatabase (dbName); Mg_col = Mg_db.GetCollection (connectionName); var query = Query. EQ (key, value); People temp = Mg_col.FindOneAs <People> (query); // obtain the People type data temp in the table based on the query conditions, which is also the data we want to update t. id = (temp as People ). id; // t is a new data of the People type. It obtains the id Mg_col.Update (query, Update. replace (t ));}
Search for objects that meet the conditions of a table and return
public T Find<T>(string dbName, string connectionName, string key, BsonValue value) {     Mg_db = mongoServer.GetDatabase(dbName);     MongoCollection collection = Mg_db.GetCollection<T>(connectionName);     var query = Query.EQ(key, value);     T result = collection.FindOneAs<T>(query);     return result; }
Queries the number of objects that meet the conditions of a table.
public long FindSameNameCollectionNum(string dbName, string collcetionName, string key, BsonValue value){    Mg_db = mongoServer.GetDatabase(dbName);    Mg_col = Mg_db.GetCollection(collcetionName);    var query = Query.EQ(key, value);    long count = Mg_col.Count(query);    return count;}
Obtains all objects in a table.
Public List <T> GetProjectArray <T> (string dbName, string connectionName) {Mg_db = Login server. getDatabase (dbName); Mg_col = Mg_db.GetCollection (connectionName); // obtain all objects in the Set var result = Mg_col.FindAllAs <T> (); list <T> pList = new List <T> (); pList. addRange (result); return pList ;}
Store audio and video text data in GridFS

Each database in MongoDB corresponds to a GridFS folder used to store audio and video texts ......
You can use a program to add
You can directly add

public void AddFileInGridFS(string dbName, string filePath){     Mg_db = mongoServer.GetDatabase(dbName);     MongoGridFS gridfs = Mg_db.GridFS;     gridfs.Upload(filePath);}
Retrieve Data in GridFS
public void LoadFileInGridFS(string dbName, string fileName){    Mg_db = mongoServer.GetDatabase(dbName);    MongoGridFS gridfs = Mg_db.GridFS;    gridfs.Download(fileName);}
Retrieve and store the data in GridFS in Byte []
public byte[] ReadFileFromfs(string dbName, string fileName, byte[] bytes){    Mg_db = mongoServer.GetDatabase(dbName);    MongoGridFS gridfs = Mg_db.GridFS;    MongoGridFSStream gridFileStream = gridfs.OpenRead(fileName);    bytes = new byte[gridFileStream.Length];    gridFileStream.Read(bytes, 0, bytes.Length);    return bytes;}
Notes

1. If the database name cannot be Chinese, an error is returned.
2. private data cannot be stored in MongoDB.
3. Add using MongoDB. Bson to the structure of the class to be saved, and add public ObjectId id {get; set ;}. this id increases after it is saved.

Experience

1. the structure database of MongoDB -- set -- object. The set here is the table, but I am talking about the "set", so that all the methods above are defined as collection, the same, I also emphasize the object concept.
2. A table can store multiple types of data, but it is best not to do this. It is very troublesome to read data. It is easy to make mistakes in case of unpacking. It is better to use a table as a type.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.