C # development interface-MongoDB,

Source: Internet
Author: User

C # development interface-MongoDB,

The C # development interface provided by MongoDB starts with downloading, and then inserts, queries, and updates.

You must first enable the MongoDB service.

Download idea.

VS: 2013, MongoDB: 3.2.0

Open VS2013, create a console program, and add two references (MongoDB. Bson. dll, MongoDB. driver. dll)

1. Insert data

Static void Main (string [] args)
{
// Database connection string
String connectionString = "mongodb: // FIG: 27017 ";
// Obtain the connection of an object instance
Var client = new clients client (connectionString );
Var server = client. GetServer ();
// Login server = Login server. Create (connectionString );
// Specify the security certificate
Required credentials credential = new required credentials ("wander", "123456 ");
// Obtain a "mongoDB" connection object
Relational Database mydb = server. GetDatabase ("test", credential );


// Declare a Collection object
Repeated collection coll = mydb. GetCollection ("t1 ");
// Apply for a document object for storing data
BsonDocument info = new BsonDocument
{
{& Quot; x & quot;, 203 },
{"Y", 102 },
{"Count", 1}
};
// Apply for a document object for storing data and using info as its embedded document
BsonDocument doc = new BsonDocument
{
{"Name", "MongoDB "},
{"Type", "database "},
{"Count", 1 },
{"Info", info}
};
// Call the insert method of collection to permanently store data to the disk.
Coll. Insert (doc );


}

Query data

Static void Main (string [] args)
{
// Database connection string
String connectionString = "mongodb: // FIG: 27017 ";
// Obtain the connection of an object instance
Var client = new clients client (connectionString );
Var server = client. GetServer ();
// Login server = Login server. Create (connectionString );
// Specify the security certificate
Required credentials credential = new required credentials ("wander", "123456 ");
// Obtain a "mongoDB" connection object
Relational Database mydb = server. GetDatabase ("test", credential );


// Declare a Collection object
Collect collection <BsonDocument> coll = mydb. GetCollection <BsonDocument> ("t1 ");
// Obtain the first data in table t1
BsonDocument bsdoc = (BsonDocument) coll. FindOne ();
Console. WriteLine (bsdoc );
Console. ReadLine ();
}

// Update data
Static void Main (string [] args)
{
// Database connection string
String connectionString = "mongodb: // FIG: 27017 ";
// Obtain the connection of an object instance
Var client = new clients client (connectionString );
Var server = client. GetServer ();
// Login server = Login server. Create (connectionString );
// Specify the security certificate
Required credentials credential = new required credentials ("wander", "123456 ");
// Obtain a "mongoDB" connection object
Relational Database mydb = server. GetDatabase ("test", credential );


// Declare a Collection object
Collect collection <BsonDocument> coll = mydb. GetCollection <BsonDocument> ("t1 ");
// Define a query object, which is equivalent to the where statement in SQL
Var queryDoc = new QueryDocument {"name", "MongoDB "}};
// Define an update object, which is equivalent to the set statement in SQL
Var updateDoc = new UpdateDocument {"$ set", new BsonDocument ("type", "NoSQL ")}};

// Pass the query object and Update object as parameters to Update
Coll. Update (queryDoc, updateDoc );
Console. ReadLine ();
}

When operating the above code, I encountered an error with the error content:

{"Command 'authenticate' failed: auth failed (response: { \"ok\" : 0.0, \"errmsg\" : \"auth failed\", \"code\" : 18 })"}
Find the cause: create a user in the Shell. After the user is authorized, it is displayed as successful and there is still a problem after the program is executed.
Solution:
Use admin
Show collections
Db. system. users. remove ({});
De. system. version. remove ({});

Db. system. version. insert ({"_ id": "authSchema", "currentVersion": 3}) Key Step
The user has been cleared. If you want to create a new user, change the User Creation method from 3.2.0:
Use test
db.createUser(   {     user: "wander",     pwd: "123456",     roles: [ ]   })
Db. auth ("wander", "123456 ");

The problem is solved.

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.