First install the monodevelop and mono development environments as described
Install mongoDB on linux according to the official mongoDB example: http://www.mongodb.org/display/DOCS/Quickstart+Unix,Note: Download different versions based on whether the CPU is 32-bit or 64-bit.
Open a terminal to start the mongoDB Database Service root @ Ubuntu:/usr/local/mongoDB/bin #./mongod
In the following process, create a database gywdb for testing. The strange thing is that mongoDB did not directly create database commands, but it was not found for a long time. Later, it was only created through indirect methods. The following command is used.
Open a new terminal: the default connected database is test.
Root @ ubuntu:/usr/local/mongoDB/bin #./mongo
MongoDB shell version: 2.0.4
Connecting to: test
Create your own database gywdb
Use gywdb
Switched to db gywdb
> Db. createCollection ("student ",{});
{"OK": 1}
In this way, the database gywdb is successfully created and a student table is created.
The following code uses C # To operate mongoDB:
Run the show dbs command to check whether the database is successfully created.
Gywdb 0.203125 GB
Local (empty)
Test 0.203125 GB
You can see that gywdb has been created.
The following describes how to perform operations on mongoDB using the C # driver code.
1. query all the databases on the server
UsingSystem;UsingSystem. Collections;UsingSystem. Collections. Generic;UsingMongoDB. Bson;UsingMongoDB. Driver;NamespaceMongoDBClient {ClassMainClass {Public Static VoidMain (String[] Args ){//MongoDb service instance connection string StringCon ="Mongodb: // localhost: 27017";//Obtain an instance connected to the mongoDB server.MongoServer server=Login server. Create (con); IEnumerable<String> Names =Server. GetDatabaseNames ();Foreach(StringNameInNames) Console. WriteLine (name); Console. ReadLine ();}}}
Running result:
2. Insert document data to the data table student.
UsingSystem;UsingSystem. Collections;UsingSystem. Collections. Generic;UsingMongoDB. Bson;UsingMongoDB. Driver;NamespaceMongoDBClient {ClassMainClass {Public Static VoidMain (String[] Args ){//MongoDb service instance connection string StringCon ="Mongodb: // localhost: 27017";//Obtain an instance connected to the mongoDB server.Login server =Login server. Create (con );//Obtain a connection object with a specific database named gywdbRelational Database mydb = server. GetDatabase ("Gywdb");//Obtain the table object in the database, that is, the student table.Collections collection mydbTable = mydb. GetCollection ("Student");//Prepare a piece of data, that is, declare a Document ObjectBsonDocument doc =NewBsonDocument {{"Name","Ling Hu shaoxia"},{"Classname","Huashan school"},{"Age",100}};//Inserts a document and persists it to the hard disk.MydbTable. Insert (doc); Console. ReadLine ();}}}
Run the command to view the result:> db. student. find ();
{"_ Id": ObjectId ("4f852ce41d41c80d9b090110"), "name": "Ling Hu shaoxia", "classname": "Huashan", "age": 100}
We can see that the table contains a piece of data just added by code. The field "_ id" is automatically generated by the system, which is equivalent to a primary key.