To manipulate a database through a program:
//Link String stringConnectionString ="mongodb://192.168.1.107"; //Database name stringDatabaseName ="LSQ"; //Collection Name stringCollectionName ="mycollection"; //Defining MONGO ServicesMongo Mongo =NewMongo (connectionString); //gets the database that corresponds to DatabaseName, does not exist and is created automaticallyMongodatabase mongodatabase = MONGO. Getdatabase (DatabaseName) asmongodatabase; //gets the CollectionName corresponding collection, which does not exist and is automatically createdmongocollection<document> mongocollection = mongodatabase.getcollection<document> (collectionName) asMongocollection<document>; //Link DatabaseMongo. Connect ();
//defines a document object that is stored in a key-value pairDocument doc =NewDocument (); doc["ID"] =1; doc["MSG"] ="helloword!"; //insert this Document object into the collectionMongocollection.insert (DOC); //finds a Id=1 Document object in the collection for a key-value pairDocument DocFind = Mongocollection.findone (NewDocument {{"ID",1 } }); //The output finds the value of the key "MSG" in the Document object, and outputsConsole.WriteLine (Convert.ToString (docfind["ID"]+" "+docfind["MSG"])); Console.read ();
MongoDB (ii)