Steps for using C # To develop MongoDB in Linux

Source: Internet
Author: User
Tags install mongodb mongodb example mongodb server

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.

  • 1
  • 2
  • 3
  • Next Page

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.