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);