Use MongoDB in C #

Source: Internet
Author: User

First, download MongoDB from the Internet. Go to http://www.mongodb.org/and find the download address of the latest website.

This is my download.

Create a folder on the E drive, decompress the downloaded ZIP file, and copy all the files in the bin directory to the folder you just created.

Create a data folder.

Then use cmd to start your MongoDB

You can check the red lines. --

 

Add MongoDB to System Service:

E: \ webdev \ MongoDB> mongod -- logpath E: \ webdev \ MongoDB \ logs \ MongoDB. log -- logappen
D -- dbpath E: \ webdev \ MongoDB \ data -- directoryperdb -- servicename MongoDB -- install

 

Access localhost: 27017 and you will see the following information, indicating that your MongoDB has been started.

 

Added:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. runtime. serialization;
Using system. Data;
Using system. Data. sqlclient;
Using MongoDB. bson;
Using MongoDB. driver;

Namespace consoleapplication1
{
Class Program
{
Static void main (string [] ARGs)
{
// Connection information
String conn = "MongoDB: // localhost ";
String database = "demobase ";
String collection = "democollection ";

Login Server MongoDB = Login server. Create (conn); // connect to the database
Repeated database connected database = MongoDB. getdatabase (database); // select the Database Name
Collections collection collections collection = collections database. getcollection (Collection); // select a collection, which is equivalent to a table

MongoDB. Connect ();

// Normal insert
VaR o = new {uid = 123, name = "xixinormal", password = "111111 "};
Collections collection. insert (O );

// Insert an object
Person P = new person {uid = 124, name = "xixiobject", password = "222222 "};
Collections collection. insert (P );

// Insert bsondocument
Bsondocument B = new bsondocument ();
B. Add ("uid", 125 );
B. Add ("name", "xixibson ");
B. Add ("password", "333333 ");
Collections collection. insert (B );

Console. Readline ();
}
}

Class person {
Public int uid;
Public string name;
Public String password;

}
}

Result:

All are written in the preceding configuration. The program automatically creates the corresponding database and set.

The following operation is not complete:

            /*---------------------------------------------
* sql : SELECT * FROM table
*---------------------------------------------
*/
MongoCursor<Person> p = mongoCollection.FindAllAs<Person>();

/*---------------------------------------------
* sql : SELECT * FROM table WHERE Uid > 10 AND Uid < 20
*---------------------------------------------
*/
QueryDocument query = new QueryDocument();
BsonDocument b = new BsonDocument();
b.Add("$gt", 10);
b.Add("$lt", 20);
query.Add("Uid", b);

MongoCursor<Person> m = mongoCollection.FindAs<Person>(query);

/*-----------------------------------------------
* sql : SELECT COUNT(*) FROM table WHERE Uid > 10 AND Uid < 20
*-----------------------------------------------
*/
long c = mongoCollection.Count(query);

/*-----------------------------------------------
* sql : SELECT Name FROM table WHERE Uid > 10 AND Uid < 20
*-----------------------------------------------
*/
QueryDocument query = new QueryDocument();
BsonDocument b = new BsonDocument();
b.Add("$gt", 10);
b.Add("$lt", 20);
query.Add("Uid", b);
FieldsDocument f = new FieldsDocument();
f.Add("Name", 1);

MongoCursor<Person> m = mongoCollection.FindAs<Person>(query).SetFields(f);
/*-----------------------------------------------
* sql : SELECT * FROM table ORDER BY Uid DESC LIMIT 10,10
*-----------------------------------------------
*/
QueryDocument query = new QueryDocument();
SortByDocument s = new SortByDocument();
s.Add("Uid", -1);//-1=DESC

MongoCursor<Person> m = mongoCollection.FindAllAs<Person>().SetSortOrder(s).SetSkip(10).SetLimit(10);

 

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.