MongoDB Usage Summary (C # Edition)

Source: Internet
Author: User

Brief introduction

MongoDB is a non-relational, document-based database with the convenience of having direct access to class types ... The official website will open free courses on a regular basis, using MongoDB in the previous project, and now I am doing a project with EF, so I would like to summarize the methods used by MongoDB, the matters of attention and some ideas, after all, it is a waste of time to piece together if you use them again.

Dll

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

Software

Very good visualization software, Mongovue

Method connection
//Mongo数据库名称private MongoDatabase Mg_db;//Mongo数据库集合名称public MongoCollection Mg_col;//MongoServerprivate MongoServer mongoServer;//连接字符串(以mongodb://开头)string"mongodb://localhost";//或者是192.xxx.xxx.xxx//string constr = "mongodb://" + ip + ":" + port;       //连接ip和该port//连接Mongo数据库varnew MongoClient(connectionString);//得到一个客户端引用mongoServer = client.GetServer();
Create a database
Mg_db = mongoServer.GetDatabase(dbName);//dbName是新库的名字//如果已有dbName这个库,那Mg_db是get这个库,否则就是创建
Create a table (collection)
Mg_col = Mg_db.GetCollection<T>(connectionName);//connectionName是新表的名字//如果该库已有connectionName这个表,那Mg_col 是get这个表,否则就是创建
Inserting data into a table
publicvoid Insert<T>(stringstring connectionName, T t)   {       Mg_db = mongoServer.GetDatabase(dbName);    //get 某个库       MongoCollection collection = Mg_db.GetCollection<T>(connectionName);    //get该库某个表       collection.Insert(t);   //插入(任意类型)   }
Delete data in a table
publicvoid delete<T>(stringstringstringvalue){    Mg_db = mongoServer.GetDatabase(dbName);    Mg_col = Mg_db.GetCollection<T>(connectionName);    varvalue);    //查询条件,eg:我要删除name是张三的该条数据,key,字段名,value是张三    Mg_col.Remove(query);}
Update one piece of data in a table
publicvoidUpData(stringstringstringvalue, People t){      Mg_db = mongoServer.GetDatabase(dbName);      Mg_col = Mg_db.GetCollection(connectionName);      varvalue);      People temp = Mg_col.FindOneAs<People>(query);      //根据查询条件获取表中People类型的数据temp,这也是我们要更新的数据      as People).id;      //t是People类型的一条新数据,它拿到要更新数据的id      Mg_col.Update(query, Update.Replace(t));}
Finds an object that meets the criteria for a table and returns
public T Find<T>(stringstringstringvalue) {     Mg_db = mongoServer.GetDatabase(dbName);     MongoCollection collection = Mg_db.GetCollection<T>(connectionName);     varvalue);     T result = collection.FindOneAs<T>(query);     return result; }
Find the number of objects that a table meets the criteria
publiclongFindSameNameCollectionNum(stringstringstringvalue){    Mg_db = mongoServer.GetDatabase(dbName);    Mg_col = Mg_db.GetCollection(collcetionName);    varvalue);    long count = Mg_col.Count(query);    return count;}
Get all the objects in a table
 Public List<T>Getprojectarray<T>(stringDbName,stringConnectionName) {mg_db=Mongoserver.Getdatabase (DbName); Mg_col=mg_db.GetCollection (ConnectionName);//Gets all objects of the collection     varResult=Mg_col.Findallas<T>();List<T>PList= New List<T>(); PList.AddRange (result);returnPList; }
Storing audio and video text data in Gridfs

Each library in MongoDB corresponds to a Gridfs folder for storing AV text ...
Can be added here with the program
Can be added directly in the Mongovue

publicvoidAddFileInGridFS(stringstring filePath){     Mg_db = mongoServer.GetDatabase(dbName);     MongoGridFS gridfs = Mg_db.GridFS;     gridfs.Upload(filePath);}
Remove the data placed in the Gridfs
publicvoidLoadFileInGridFS(stringstring fileName){    Mg_db = mongoServer.GetDatabase(dbName);    MongoGridFS gridfs = Mg_db.GridFS;    gridfs.Download(fileName);}
Take out the data placed in the Gridfs and save it in byte[]
publicbyteReadFileFromfs(stringstringbyte[] bytes){    Mg_db = mongoServer.GetDatabase(dbName);    MongoGridFS gridfs = Mg_db.GridFS;    MongoGridFSStream gridFileStream = gridfs.OpenRead(fileName);    newbyte[gridFileStream.Length];    0, bytes.Length);    return bytes;}
Precautions

1. The library name can not be in Chinese, will be error.
2.private of data cannot be stored in MongoDB.
3. To deposit the class, add the using Mongodb.bson in the structure, and add the public ObjectId ID {get; set;}, this ID has been deposited since the growth.

Experience

1.MongoDB Structure Library--collection--object, where the set is the table, but I say "set", so that the above methods I define is collection, the same, I emphasize the concept of objects
2. A table can store multiple types of data, but it is best not to do so, read the time is very troublesome, unpacking the box is very easy to wrong, or a table a type of good.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MongoDB Usage Summary (C # Edition)

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.