ASP Net Core Fro MongoDB Part1

Source: Internet
Author: User
Tags asp net

"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

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.