MongoDB provides a C # development interface, starting with the download, then inserting, querying, updating.
Start the MongoDB service first.
Download Http://github.com/mongodb/mongo-csharp-driver/downloads. Each version will have two drive modes, one is. msi and one is. zip.
vs:2013,mongodb:3.2.0
Open VS2013, create a new console program, and add two references (Mongodb.bson.dll,mongodb.driver.dll) to download.
1. Inserting data
static void Main (string[] args)
{
Database connection string
String connectionString = "mongodb://127.0.0.1:27017";
Get a connection to an object instance
var client = new Mongoclient (connectionString);
var server = client. Getserver ();
Mongoserver Server = Mongoserver.create (connectionString);
Specifying a security certificate
Mongocredentials credential = new Mongocredentials ("Wander", "123456");
Get a "MongoDB" Connection object
Mongodatabase mydb = server. Getdatabase ("Test", credential);
Declares a Collection object
Mongocollection coll = mydb. GetCollection ("T1");
Request a Document object for storing data
Bsondocument info = new Bsondocument
{
{"X", 203},
{"Y", 102},
{"Count", 1}
};
Request a Document object to store the data and use info as its embedded document
Bsondocument doc = new bsondocument
{
{"Name", "MongoDB"},
{"Type", "Database"},
{"Count", 1},
{"Info", info}
};
Call collection's Insert method to permanently store the data on disk
Coll. Insert (DOC);
}
Querying data
static void Main (string[] args)
{
Database connection string
String connectionString = "mongodb://127.0.0.1:27017";
Get a connection to an object instance
var client = new Mongoclient (connectionString);
var server = client. Getserver ();
Mongoserver Server = Mongoserver.create (connectionString);
Specifying a security certificate
Mongocredentials credential = new Mongocredentials ("Wander", "123456");
Get a "MongoDB" Connection object
Mongodatabase mydb = server. Getdatabase ("Test", credential);
Declares a Collection object
mongocollection<bsondocument> coll = mydb. Getcollection<bsondocument> ("T1");
Get the first piece of data for the T1 table
Bsondocument Bsdoc = (bsondocument) Coll. FindOne ();
Console.WriteLine (Bsdoc);
Console.ReadLine ();
}
Update data
static void Main (string[] args)
{
Database connection string
String connectionString = "mongodb://127.0.0.1:27017";
Get a connection to an object instance
var client = new Mongoclient (connectionString);
var server = client. Getserver ();
Mongoserver Server = Mongoserver.create (connectionString);
Specifying a security certificate
Mongocredentials credential = new Mongocredentials ("Wander", "123456");
Get a "MongoDB" Connection object
Mongodatabase mydb = server. Getdatabase ("Test", credential);
Declares a Collection object
mongocollection<bsondocument> coll = mydb. Getcollection<bsondocument> ("T1");
Defines a query object equivalent to the WHERE statement in SQL
var Querydoc = new Querydocument {{"name", "MongoDB"}};
Defines an update object, equivalent to a SET statement in SQL
var Updatedoc = new UpdateDocument {{"$set", New Bsondocument ("type", "NoSQL")}};
To complete the update by passing the query object and the update object as parameters to update
Coll. Update (Querydoc, Updatedoc);
Console.ReadLine ();
}
I encountered an error while manipulating the above code, and the error content was
{"Command ‘authenticate‘ failed: auth failed (response: { \"ok\" : 0.0, \"errmsg\" : \"auth failed\", \"code\" : 18 })"}
Find the reason: in the shell to create a user, authorized users, the display is successful, after the execution of the program is still a problem.
Workaround:
Use admin
Show collections
Db.system.users.remove ({});
De.system.version.remove ({});
db.system.version.insert({ "_id" : "authSchema", "currentVersion" : 3 })关键的一步
The user has been emptied, and now to be newly established, create the user mode from 3.2.0 to:
Use test
DB. CreateUser( { user"Wander"pwd"123456"roles})
db.auth("wander","123456");
问题解决了。
C # Development Interface--mongodb