Simulating the real environment of building Web projects (vi)

Source: Internet
Author: User
Tags mongodb driver redis server

today, the operation of Redis and MongoDB tidy up, easy to see later on their own, nonsense not much to say, directly into the Theme.

first, Redis

1. Quoting StackExchange.Redis.dll

Since Servicestack.redis is a commercial version of more than 4.0, it has to charge

So here I chose the Stackexchange.redis to apply to the Project.

First you need to get StackExchange.Redis.dll, here are a few ways to Get:

① Direct Download Stackexchange.redis.dll run (recommended, simple and Convenient)

② to the Redis website, download the C # version of the client, then run the build locally and build the Stackexchange.redis.dll

(little Wolf several times due to network instability, Compile-time in the process of restoring the NuGet package is always not passed, resulting in a problem with the Dll)

③ in vs, install Stackexchange.redis library with NuGet

(with NuGet installed, There will be more libraries in the framework that may not be needed, increasing the size of the Project)

Here I provide a self-uploaded stackexchange.redis.dll, the need for friends can Download.

Baidu Network Disk: Http://pan.baidu.com/s/1qXUM39M password: VNRZ

Download the file to be referenced in the project after pressurization, and then add it in the Code:

Using stackexchange.redis;

2. Create a Conn Object

Connectionmultiplexer conn = Connectionmultiplexer.connect ("192.168.1.111,password=admin");

The 192.168.1.111 above is the IP address of the Redis service, and admin is the password for the Redis service.

3. Create a DB object

Idatabase DB = Conn. Getdatabase (0);

Parameter 0 represents the 0 database selected for the Redis server

4. The operation of Redis can be achieved with the object db

In the next article, I enclose a small case that simply encapsulates the operation of Redis and mongodb,

Interested can be downloaded to See. There is no way to introduce the method of the db one by One.

second, MongoDB Driver

1. Download C # driver from MONGO official website

Website Address: http://mongodb.github.io/mongo-csharp-driver/2.2/getting_started/installation/

Baidu Network Disk: Http://pan.baidu.com/s/1c2v9tIS password: lt8m

2. Unzip the downloaded driver and refer to three files in the Project:

MongoDB.Bson.dll

MongoDB.Driver.dll

MongoDB.Driver.Core.dll

3. Add in the program Code:

Using mongodb.bson;

Using mongodb.driver;

4. Create a Client object

Imongoclient client = new Mongoclient ("mongodb://192.168.1.111:27017");

5. Create DATABASE object

Imongodatabase db = Client. Getdatabase ("db_sys");

6. Create a Collection Object

Imongocollection<userentity> collection = Db. Getcollection<userentity> ("user");

7. Filter Criteria

Before introducing crud, Let's introduce some important syntax in the filter Condition.

Here we treat a filter condition as an atom,

Set to $ A (in the class library behind the Wolf package is also designed according to this Principle)

(1) equals (Equal)

$a = Key:value

Case

Query data: old wolf, Xiao yang, c+, antoine, son, xiaojing, old Wolf big Fool

Query condition: key: ' old Wolf '}

Search Result: old Wolf

(2) Greater than (Greater)

$a = key: {$gt: value}

Case

Query Data: 20, 21, 22, 23

Query condition: key: {$gt: 21}

Query Results: 22, 23

(3) smaller than (less)

$a = key: {$lt: value}

Case

Query Data: 20, 21, 22, 23

Query condition: key: {$lt: 21}

Query Results: 20

(4) greater than or equal to (greateorequal)

$a = key: {$gte: value}

Case

Query Data: 20, 21, 22, 23

Query condition: key: {$gte: 21}

Query Results: 21, 22, 23

(5) less than or equal (lessorequal)

$a = key: {$lte: value}

Case

Query Data: 20, 21, 22, 23

Query condition: key: {$lte: 21}

Query Results: 20, 21

(6) Not equal to (notequal)

$a = key: {$ne: value}

Case

Query Data: 20, 21, 22, 23

Query condition: key: {$ne: 21}

Query Results: 20, 22, 23

(7) Opening interval (between)

$a = key: {$gt: value0, &lt:value1}

Case

Query Data: 20, 21, 22, 23

Query condition: key: {$gt: &lt:23}

Query Results: 22

(8) contains (in)

$a = key: {$in: array}

Case

Query Data: 0, 1, 2, 3

Query condition: key: {$in: [+]}

Query Results: 1, 2

(9) not included (notin)

$a = key: {$nin: array}

Case

Query Data: 0, 1, 2, 3

Query condition: key: {$nin: [+]}

Query Results: 0, 3

(10) Fuzzy Query (like)

① must begin with the specified value: key:/^value/

② must have a different value before the specified value: key:/.value/

③ contains the specified value: key:/value/

Case

Query Data: small wolves, wolves, wolves, den, Grey wolf, sirius, Xiao Yang

Query Criteria:

① $a = key:/^ wolf/

② $a = key:/. wolf/

③ $a = key:/wolf/

Query Result:

① Wolf group, den

② wolf, old wolf, Grey wolf, Sirius

③ wolf, old wolf, wolf, den, Grey wolf, Sirius

In fact, you can see that the first and second collections are the third Kind

(one) Logic and (and)

Suppose $ A = key:value,

Then $c = $a, $b

(12) logic or (or)

Suppose $ A = key:value,

Then $c = $or: [{$a}, {$b}]

8. Pagination Sorting Query

Collection. Find (filter, null). Sort ("{ctime:-1}"). Skip ((index-1) * size). Limit (size). Tolist<t> ();

9. Create

userentity entity = new Userentity ();

Collection. Insertone (entity);

10. Update

Filter: Filters on Update

Update: What's Updated

Collection. Updateone (filter, update);

11. Delete

Collection. Deleteone ("{_id: ' 00010001 '}");

Introduction to Mongo-driven crud the next article introduces a small case,

is mainly a User Details table additions and deletions, mainly to the use of mongo-based driving,

One of the additions and deletions to use is mongodb, and the user ID of the self-added to the redis,

Finally recommended a senior summary of an article, written in very detailed:

Http://www.cnblogs.com/wuhuacong/archive/2016/01/05/5098348.html

Simulating the real environment of building Web projects (vi)

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.