Usage of MongoDB Query

Source: Internet
Author: User
Tags mongodb query

To use MongoDB in C #, you first have to have a mongodb-supported C # version of the driver. The C # version of the driver seems to have many kinds, such as the official Samus. The idea of implementation is mostly similar. Here we use the officially provided Mongo-csharp-driver:

: Http://github.com/mongodb/mongo-csharp-driver/downloads

Get two DLLs after decompression:

MongoDB.Driver.dll: As the name implies, the driver

MongoDB.Bson.dll: serialization, JSON-related

We then refer to these two DLLs in our program.

2. Connect to the database

[CSharp]View PlainCopy 
    1. Using Mongodb.bson;
    2. Using Mongodb.driver;



[CSharp]View PlainCopy 
  1. Database connection string
  2. Const String strconn = "mongodb://127.0.0.1:27017";
  3. Database name
  4. Const String dbName = "Test";
  5. Mongoserver server;
  6. Mongodatabase DB;
  7. void Init ()
  8. {
  9. //Create a database link
  10. Server = MongoDB.Driver.MongoServer.Create (strconn);
  11. //Access to the database
  12. db = Server. Getdatabase (DbName);
  13. }

3. Query data

1) Query rule collection

[CSharp]View PlainCopy 
  1. Class Users
  2. {
  3. Public ObjectId _id; //bsontype.objectid this corresponds to MongoDB.Bson.ObjectId.
  4. public string Name { get;  set;}
  5. public string Sex { set;  get;}
  6. }
  7. Const String ENTER = "\ r \ n";
  8. Private void Btnquery_click (object sender, EventArgs e)
  9. {
  10. //Get the Users collection
  11. Mongocollection col = db.  GetCollection ("Users");
  12. //Query the data in all collections
  13. var result1 = Col. Findallas<users> (). ToList ();
  14. StringBuilder sb = new StringBuilder ("");
  15. foreach (Users user in (list<users>) result1)
  16. {
  17. Sb. Append (user. Name + ENTER);
  18. }
  19. MessageBox.Show (sb.) ToString ());
  20. }


This approach is only suitable for collections where each document structure is identical:

But one of the arguments for NoSQL's criticism of relational databases is that this so-called structure of each record should be consistent, because some fields are not required for all records. So, in MongoDB here, for the collection of Chinese document structure is inconsistent, the above code gets no data, it seems that the following methods should be used:

2) Query Irregular collection

Set Condition:

In the collection thins, basically each document structure is different.

Find the following code:

[CSharp]View PlainCopy  
    1. Private void Btnquery2_click (object sender, EventArgs e)
    2. {
    3. //Get Thins collection
    4. Mongocollection col = db.  GetCollection ("Thins");
    5. //Query the data in all collections
    6. var result1 = Col. Findallas<bsondocument> (). ToList ();
    7. StringBuilder sb = new StringBuilder ("");
    8. foreach (bsondocument bd in (list<bsondocument>) result1)
    9. {
    10. String name = Bd. Contains ("name")? Bd. GetValue ("name").  ToString (): "does not exist";
    11. Sb. Append (name + ENTER);
    12. }
    13. MessageBox.Show (sb.) ToString ());
    14. }

Usage of MongoDB Query

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.