MongoDB samus Driver (2), mongodbsamus driver

Source: Internet
Author: User

MongoDB samus Driver (2), mongodbsamus driver

The driver connection method mentioned in the previous article. This article provides the complete link code and instance

Complete database link

1 static IMongo _ mongo = null; 2 public writable drive () // This class integrates the IDisposable Interface 3 {4 if (_ mongo = null) 5 {6 var connectionString = new mongoonstringbuilder () 7 {8 ConnectionTimeout = TimeSpan. fromSeconds (30), 9 ConnectionLifetime = TimeSpan. fromSeconds (10), 10 MinimumPoolSize = 1, 11 MaximumPoolSize = 10, 12 Pooled = true, 13}; 14 connectionString. addServer (new MongoServerEndPoint ("127.0.0.1", 27017); 15 var config = new mongo. configuration. configure configuration (); 16 config. connectionString = connectionString. toString (); 17 config. readLocalTime = false; 18 _ mongo = new Mongo (config); 19 _ mongo. connect (); 20} 21} 22 23 public static IMongo Driver24 {25 get26 {27 return _ mongo; 28} 29}

 

Add data:

1 using (IDocumentDrive db = new MongoDB. snapshot drive () 2 {3 db. driver. collection ("docs "). insert (4 new Document ("uid", 123456) 5. merge ("uName", "admin") 6. merge ("age", new Document ("Last August", DateTime. now) // The Document value is of any type, 7. merge ("addtime", DateTime. utcNow) 8); 9}
Document is defined by the samus driver. It is actually an encapsulation of IDictionary <string, object>.

Delete data:
1 using (IDocumentDrive db = new MongoDB. snapshot drive () 2 {3 db. driver. collection ("docs "). remove (new mongo. document ("uid", 123456); // Delete the Document with UID = 123456 4}

Update Data:

1 using (IDocumentDrive db = new MongoDB.MongoDrive())2 {3     db.Driver.Collection("docs").Update(new mongo.Document("uName", "drop attr"), new mongo.Document("uid", 123456));4 }

Update: In addition to Update, there is also an UpdateAll method, which is not explained too much.

 

Add, delete, and modify the query.

Simple data query:

Document doc = db. collection ("docs "). findOne (new mongo. document ("uid", 12346); // query a Document. All its attributes and subdocuments cannot be found. The data returned value is of the Document type.
Use Time:
Var age = doc ["age"] // because Document is a Dictionary, you can directly use the value of the mid-index.
Doc ["uName"] = "mongdb samus"; // The Value assignment also uses the indexer.
Db. Collection ("docs"). Update (doc, new mongo. Document ("uid", 123456); // Update this Document later.


Db. collection ("docs "). findAll (). statements // FindAll is to query all Documents. FindAll () Only returns the cursor and calls FindAll (). users can get the value officially,
FindAll (). Sort (). Skip (). Limit () // there are several other methods for FindAll. Sort: Sort Skip: Skip the number of Limit: Number of value entries
Sort ("age", MongoDB. IndexOrder. Descending) // Sort
Skip (27) // Skip 27
Limit (28) // obtain 28

FindAndModify: this function is similar to UPDATE,
FindAndModify (new document structure, search condition, returnNew = true/false) // search for a document based on the condition. If it is found, modify the document to the new document format and return the document. The new document is returned, or the old document before modification, determined by the returnNew Parameter
Other parameters, such as upsert, are used to update only the first matching item, or to update all matching items.

 

Flexible query:

db.Collection("docs").Find(new Document("age", 27)).Documents;
Find: functions are used most in projects, mainly for flexibility and heavy load.
Find (new Document ("age", 27), int limit, int skip) // query by PAGE
Find (new Document ("age", 27) // query Conditions
Like the FindAll function, you must call the statements to obtain the values. Otherwise, you can only obtain the cursor.


Find (Document) // in this form, all the structure of the Document is returned. When querying a user, the user password is returned, and the second parameter needs to be passed.
Find (new Document ("age", 27), new Document ("age ",""). merge ("uid", "EMPTY string") // at this time, the returned document format only contains two attributes: age and uid.

We can see that we only need to pass the Document to Find and then accept the data. The complicated thing is how to build the Document,

The following describes how to create common query conditions.

The samus driver has an Op class which defines conditional operator functions and can be directly used.

Example:

Op. GreaterThan (27) // The generated $ gt // is greater

Op. GreaterThanOrEqual (27) // The $ gte of mongoDB is generated. // The value is greater than or equal

Op. In (new string [] {"123465", "123457", "132458"}) // IN Operation

According to the name, we can directly see what the specific meaning is. Not all of them are annotated.

There is no ready-made Like function in Op. I have extended the following:

1         public static Document Like(string key)2         {3             return new Document().Merge(new Op().Add("$regex", key)).Merge(new Op().Add("$options", "$i"));4         }

How to Use Op to construct conditional queries:

// Only use examples. The conditions must never be met.

New Document ("uid", "123456") // uid = 123456. Merge ("age", Op. GreaterThan (27) // and age> 27
. Merge ("addtime", Op. GreaterThan (DateTime. Now) // and addtime> current time Description: Op. GreaterThan can be of different types, not necessarily int
. Merge ("age", new Document (Op. greaterThan (15 ). merge (Op. lessThanOrEqual (40) // and (age> 15 & age <40)
. Merge ("uid", new Document ("$ nin", new string [] {"123", "124", "124 "})) // and the uid is not in the array.
. Merge ("uid", new Document ("$ in", new string [] {"666", "777", "888 "})) // and the uid is in 666,777,888.
. Merge ("face. big "," http://www.salsnet.win/1.jpg ") // search for sub-documents: the document format is probably {'uname': 'abc'," face ": {" small ":" 111.jpg ", "big": "222.jpg "}}

Basically, this is all about building query documents,

 

Group query, joint query, advanced query, and more next time

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.