C # add or delete a MongoDB Database

Source: Internet
Author: User
Tags mongodb server

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using MongoDB. Driver;
Using MongoDB. Bson;

Namespace mangodb
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

Private void btn_import_Click (object sender, EventArgs e)
{
// MongoDB server connection string
String connectionString = "mongodb: // localhost: 27017 ";
Login server = Login server. Create (connectionString );
// Connect to the mongodb_c_demo Database
Relational Database db = server. GetDatabase ("mongodb_c_demo ");
// Obtain the set fruit
Collections collection = db. GetCollection ("websiteUrl ");
InserData (collection );

// Query all
// Collection. FindAllAs (Type. get );

// Export cursor cur = collection. FindAllAs (Type. d
// While (cur .()){
// System. out. println (cur. next ());
//}

// The above code inserts two pieces of data into the fruit table.
// Insert into db_c_demo.fruit (name, color)
// Values ('apple', 'red'), ('bana', 'yellow ');
}

Private void btn_sel_Click (object sender, EventArgs e)
{
// MongoDB server connection string
String connectionString = "mongodb: // localhost: 27017 ";
Login server = Login server. Create (connectionString );
// Connect to the mongodb_c_demo Database
Relational Database db = server. GetDatabase ("mongodb_c_demo ");
// Obtain the set fruit
Collections collection = db. GetCollection ("websiteUrl ");
SelectAll (collection );
}

Private void btn_del_Click (object sender, EventArgs e)
{
// MongoDB server connection string
String connectionString = "mongodb: // localhost: 27017 ";
Login server = Login server. Create (connectionString );
// Connect to the mongodb_c_demo Database
Relational Database db = server. GetDatabase ("mongodb_c_demo ");
// Obtain the set fruit
Collections collection = db. GetCollection ("websiteUrl ");
DelData (collection );
}

Private void btn_update_Click (object sender, EventArgs e)
{
// MongoDB server connection string
String connectionString = "mongodb: // localhost: 27017 ";
Login server = Login server. Create (connectionString );
// Connect to the mongodb_c_demo Database
Relational Database db = server. GetDatabase ("mongodb_c_demo ");
// Obtain the set fruit
Collections collection = db. GetCollection ("websiteUrl ");
Update (collection );
}

Private void btn_docToDatabase_Click (object sender, EventArgs e)
{

}

/// <Summary>
/// Insert data
/// </Summary>
/// <Param name = "collection"> </param>
Private void inserData (custom collection)
{

// Create the object fruit_1
BsonDocument fruit_1 = new BsonDocument
{
{"_ Id", 10001 },
{"Website", "http://www.my400800.cn
"},
{"Name", "400 phone "},
{"Count", 10 },
{"Time", DateTime. Now}
};
// Create the object fruit_2
BsonDocument fruit_2 = new BsonDocument
{
{"_ Id", 10002 },
{"Website", "http://www.hrxc.net
"},
{"Name", "Huaren trustful "},
{"Count", 20 },
{"Time", DateTime. Now}

};
// Put the object fruit_1 in the set fruit
Collection. Insert (fruit_1 );
// Put the object fruit_2 in the set fruit
Collection. Insert (fruit_2 );

}

/// <Summary>
/// Data Retrieval
/// </Summary>
/// <Param name = "coll"> </param>
Private void selectAll (invalid collection coll)
{

News firstNews = coll. FindOneAs <News> ();
// Search for the first document
QueryDocument query = new QueryDocument ();
// Define the query document
Query. Add ("_ id", 10001 );
Query. Add ("count", 10 );
Export cursor <News> qNews = coll. FindAs <News> (query );
Foreach (News oneRecGouRu in qNews)
{
Console. WriteLine (oneRecGouRu. name. ToString () + "\ r \ n ");
}

BsonDocument bd = new BsonDocument ();
// Define the query document count> 2 and count <= 4
Bd. Add ("$ gt", 2 );
Bd. Add ("$ lte", 10 );
QueryDocument query_a = new QueryDocument ();
Query_a.Add ("count", bd );

FieldsDocument fd = new FieldsDocument ();
Fd. Add ("_ id", 0 );
Fd. Add ("count", 1 );
Fd. Add ("time", 1 );

Export cursor <News> mNewss = coll. FindAs <News> (query_a). SetFields (fd );
// Only count and time are returned.
Var time = BsonDateTime. Create (Convert. ToDateTime ("23:26:00 "));
BsonDocument db_t = new BsonDocument ();
Db_t.Add ("$ gt", time );
QueryDocument qd_3 = new QueryDocument ();
Qd_3.Add ("time", db_t );

Export cursor <News> mNkews = coll. FindAs <News> (qd_3 );

}

/// <Summary>
/// Delete data
/// </Summary>
/// <Param name = "coll"> </param>
Private void delData (invalid collection coll)
{
QueryDocument query_a = new QueryDocument ();
Query_a.Add ("count", 10 );
Coll. Remove (query_a );

}

Private void update (invalid collection coll)
{
News customer = new News ();
Customer. name = "Call Center ";
Customer. website = "http://www.hrxc.net ";
Customer. time = DateTime. Now;
// Search for the first document
QueryDocument query = new QueryDocument ();
IList <BsonElement> elements = new List <BsonElement> ();
// Define the query document
Query. Add ("_ id", 10001 );
Query. Add ("count", 10 );
BsonDocument doc = BsonExtensionMethods. ToBsonDocument (customer );
Foreach (var item in doc. Elements ){
If (item. Value. IsBsonNull | item. Name = "_ id ")
{
Elements. Add (item );
}
}

Foreach (var el in elements ){
Doc. RemoveElement (el );
}

Var update = new UpdateDocument (doc );
Coll. Update (query, update );

// Coll. Update (customer, (xw => xw. name = ''));
// Coll. Update (customer, (x => x. = customer. CustomerID ));
// Coll. Update (null, IMongoUpdate.

}




}

Public class News
{

Public int _ id {get; set ;}
Public string website {get; set ;}
Public string name {get; set ;}
Public int count {get; set ;}
Public DateTime time {get; set ;}

}
}

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.