MongoDB Learning Note Four C # call MongoDB

Source: Internet
Author: User
Tags findone mongoclient reserved

Driven
    • Download
      Https://github.com/mongodb/mongo-csharp-driver/downloads
    • Project Address:
      Https://github.com/mongodb/mongo-csharp-driver

    • 1.10 Use reference:
      http://mongodb.github.io/mongo-csharp-driver/1.10/

    • 1.10 API
      http://api.mongodb.org/csharp/1.10/

      The official recommendation is to install with NuGet. But I search out too many things, not clear, so direct download.
      It is important to note that 2.0 and 1.10 support different environments.

c#/.net Driver Version MongoDB 2.4 MongoDB 2.6 MongoDB 3.0
Version 2.0 ? ? ?
Version 1.10 ? ? ?
Driver Version . NET 3.5 . NET 4.0 . NET 4.5 Mono 2.10 Mono 3.x
Version 2.0 ? ?
Version 1.10 ? ? ? ? ?

I am here to download version 1.10, Framework 4.0 environment.

Use connection
Using Mongodb.bson;Using Mongodb.driver;var client =New Mongoclient ("mongodb://localhost:27017");var server = client. Getserver (); var database = server. Getdatabase ("foo");  var collection = database. Getcollection<bsondocument> ("Bar");  Await collection. Insertoneasync (new Bsondocument ("Name", "Jack"));  var list = await collection. Find (new Bsondocument ("Name", "Jack")). Tolistasync (); foreach (var document in list) {Console.WriteLine (document["Name"]);}     
Document is an entity class
Using Mongodb.bson;Using Mongodb.driver;PublicClass person{Public ObjectId Id {GetSet }PublicString Name {GetSet }}var client = New Mongoclient ("mongodb://localhost:27017");  var server = client. Getserver (); var database = server. Getdatabase ("foo");  var collection = database. Getcollection<person> ("Bar");  Await collection. Insertoneasync (new Person {Name = "Jack"});  var list = await collection. Find (x = X.name = = "Jack"). Tolistasync (); foreach (var in list) {Console.WriteLine (person). Name);}                
The entity class is used for the following code
public class Entity{    public ObjectId Id { get; set; } public string Name { get; set; }}

Insert
var entity = new Entity { Name = "Tom" };collection.Insert(entity);var id = entity.Id; // Insert will set the Id if necessary (as it was in this example)
Find
var query = Query<entity>. EQ (E= = E.id, Id);var entity = collection. FindOne (query);//var entity = collection. Findonebyidas<entity> (ID);/* Define a query: query stdid=1 document Findoneargs args = new Findoneargs {Query = Query.eq (" stdID ", 1), Span class= "hljs-regexp" >//query stdid field equals 1 of document. }; //query var std = collection. Findoneas<student> (args); *//* queries multiple imongoquery query = Query.gte ( "stdID", 2); var result=collection. Findas<student> (Query.gte (2)); foreach (var each in result) {Console.WriteLine (each.stdname);} */ 

Save
entity.Name = "Dick";collection.Save(entity);
Update
var query = Query<Entity>.EQ(e => e.Id, id);var update = Update<Entity>.Set(e => e.Name, "Harry"); // update modifierscollection.Update(query, update);

Delete
var query = Query<Entity>.EQ(e => e.Id, id);collection.Remove(query);

This is a complete example:
Using System;Using System.Collections.Generic;Using System.Linq;Using System.Text;Using Mongodb.bson;Using Mongodb.driver;Using Mongodb.driver.builders;namespace consoleapplication1{PublicClass Entity {Public ObjectId Id {GetSet }PublicString Name {GetSet }} Class Program {Staticvoid Main (String[] (args) {var connectionString ="Mongodb://localhost";var client =New Mongoclient (connectionString);var server = client. Getserver (); var database = server. Getdatabase ("test"); var collection = database. Getcollection<entity> ("entities"); var entity = New Entity {Name = "Tom"}; collection. Insert (entity); var id = entity. Id; var query = Query<entity>. EQ (E = e.id, Id); Entity = collection. FindOne (query); Entity. Name = "Dick"; collection. Save (entity); var update = Update<entity>. Set (E = e.name, "Harry"); collection. Update (query, update); Collection. Remove (query); } }}

Reference: http://mongodb.github.io/mongo-csharp-driver/1.10/getting_started/

LINQ Query

The C # driver 1.8 version began to support LINQ queries.

in collection.AsQueryable<SY.Model.User>()                       //where e.age> 22 select e;linquery=linquery.Where(c=>c.Name=="张三");int count=linquery.Count();

Reference: http://mongodb.github.io/mongo-csharp-driver/1.10/linq/

Document Query method

Not tested, the reference address is recorded here.

Document DocName = new Document {{"Field name 1", "Input value 1"}, {"Field Name 2", "Input value 2"};///<summary>Get user information by name///</summary>///<param name= "Muserinfo" > User model Class</param>///<returns> User-Generic collection</returns>Public list<userinfo>Getuserbyname (UserInfo muserinfo) {list<userinfo> Lsuser =New List<userinfo> ();using (Mongo Mongo =New Mongo ("Mongodb://localhost")) {mongodatabase mongodatabase = MONGO. Getdatabase ("UserInfo")As Mongodatabase; mongocollection<document> mongocollection = mongodatabase.getcollection<document> ("MyCollection")As mongocollection<document>; Mongo. Connect (); Document DocName =New Document {{ "FirstName",  "AAA"}, { "LastName", Span class= "hljs-string" > "BBB"}; mongodb.icursor<document> users = Mongocollection.find (DocName); foreach (Document user in users. Documents) {UserInfo Muser = new UserInfo (); muser.firstname = User[" FirstName "]. ToString (); Muser.lastname = User[ "LastName"]. ToString (); Muser.corporationname = User[ "Corporationname"]. ToString (); Muser.phone = User[ "Phone"]. ToString (); Muser.email = User[ "Email"]. ToString (); Muser.usertype = User[ "usertype"]. ToString (); Lsuser.add (Muser); }} return lsuser;}           

http://weishangxue.blog.163.com/blog/static/21575188201181633811102/
Http://mikaelkoskinen.net/post/mongodb-aggregation-framework-examples-in-c

MongoDB Learning Note Four C # call MongoDB

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.