"There's no time!" "
This learning MongoDB, refer to the following articles:
Using MongoDB with ASP. Core–part II (Implementation)
From
MongoDB Study Notes ( two ) through Samus drive for basic data operations
From
Omit how to install and debug MONGOBD, see the two articles above.
before you do the following, you must determine MongoDB Service is turned on, do not know how to open the service, please see MongoDB installation and configuration.
Download driver, new console project (. NET Core)
and add the MongoDB.dll 's Reference, NuGet searches MongoDB (current version 2.4.3).
Private Imongodatabase _database; private string _connstr = "mongodb://localhost:27017"; private string _dbname = "Vesseldb"; |
Create a database proxy class, link a database
Public Vesselrepository () { var client = new Mongoclient (_CONNSTR); _database = client. Getdatabase (_dbname); } |
defining an Object
public class Vessel { [Bsonid] public int IMO {get; set;} [Bsonelement ("Vesselname")] public string Name {get; set;} [Bsonelement ("Portofregistry")] public string Registry {get; set;} [Bsonelement ("Yearofbuilt")] Public DateTime yearofbuilt {get; set;} } |
The method of adding and modifying the deletion
Addmethod public void Addvessel (Vessel VSL) { _database. Getcollection<vessel> ("vessels"). Insertone (VSL); } Deletedmethod public void Deletevessel (string vslname) { var filter = Builders<vessel>. Filter.eq (VSL = VSL). Name, Vslname); _database. Getcollection<vessel> ("vessels"). Deleteone (filter); } Getmentod Public ienumerable<vessel> Getallvessels () { Return _database. Getcollection<vessel> ("vessels"). Find (FILTERDEFINITION<VESSEL>. Empty). ToList (); } UpdateMethod public void Updatevessel (int IMO, Vessel VSL) { var filter = Builders<vessel>. Filter.eq (v = v.imo, IMO); var update = Builders<vessel>. Update . Set (v = v.name, VSL. Name) . Set (v = v.registry, VSL. Registry) . Set (v = v.yearofbuilt, VSL. Yearofbuilt); _database. Getcollection<vessel> ("vessels"). Updateone (filter, update); } |
It works perfectly.
Asp. Net Core Fro MongoDB Part1