Use MongoDB's support for the LINQ drive norm

Source: Internet
Author: User

MongoDB has several drivers. The open-source project of the Distributed File storage database MongoDB uses github.com/samus/mongodb-csharp. monogodb-csharpis not a strong driver and is easy to use. Use the norm C # driver that supports strong access to MongoDB. One difference between the norm driver and MongoDB-CSHARP is that norm uses a strongly typed class to operate the document class of MongoDB-CSHARP.

It's easy to use norm, just reference norm. dll. The following example is a consoleProgram:

Model class, representing the data saved to the database

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using norm;

Namespace firstmongodb
{
Public Class Customer: ihaveidentifier
{
Public objectid _ id {Get; set ;}
Public string name {Get; set ;}
Public datetime lastorderutc {Get; set ;}
Public list <string> ordereditems {Get; set ;}

Public customer ()
{
Ordereditems = new list <string> ();
}
}
}

This is simple enough. Next we will connect to the database.

Public class mongodbdatacontext: idisposable
{
Private readonly extends queryprovider provider;

Public writable queryprovider provider
{
Get {return provider ;}
}

Public static string databasename {Get; set ;}

Public mongodbdatacontext ()
{
If (string. isnullorempty (databasename ))
{
Throw new invalidoperationexception ("You must set the static databasename property .");
}
Provider = new jsonqueryprovider (
New Mongo (databasename, "127.0.0.1", "27017", null ));
}

A database name, server address, and port need to be provided to connect to data using norm in winter. Refer to the above redCode.

Insert an object to the database

Private Static void insert ()
{

Customer c = new customer ();
C. Name = "Jake ";
C. lastorderutc = datetime. utcnow;
C. ordereditems. Add ("frappa ");
C. ordereditems. Add ("beer ");
C. ordereditems. Add ("redbull! ");
C. ordereditems. Add ("Wings ");

Using (mongodbdatacontext CTX = new mongodbdatacontext ())
{
CTX. Add (C );
}
}

Query a database using LINQ

Using (mongodbdatacontext CTX = new mongodbdatacontext ())
{
VaR query =
From C in CTX. MERs
Where C. Name = "Michael"
Orderby C. lastorderutc descending
Select C;

Foreach (VAR customer in query)
{
Console. writeline ("{0} bought {1 }",
Customer. Name,
Customer. ordereditems. firstordefault ()
);
}
}

 

Reference: Using MongoDB with the norm driver

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.