C# mongodb常用查詢


官方驅動查詢:

Query.All("name", "a", "b");//通過多個元素來匹配數組
 
 Query.And(Query.EQ("name", "a"), Query.EQ("title", "t"));//同時滿足多個條件
 
 Query.EQ("name", "a");//等於
 
 Query.Exists("type", true);//判斷鍵值是否存在
 
 Query.GT("value", 2);//大於>
 
 Query.GTE("value", 3);//大於等於>=
 
 Query.In("name", "a", "b");//包括指定的所有值,可以指定不同類型的條件和值
 
 Query.LT("value", 9);//小於<
 
 Query.LTE("value", 8);//小於等於<=
 
 Query.Mod("value", 3, 1);//將查詢值除以第一個給定值,若余數等於第二個給定值則返回該結果
 
 Query.NE("name", "c");//不等於
 
 Query.Nor(Array);//不包括數組中的值
 
 Query.Not("name");//元素條件語句
 
 Query.NotIn("name", "a", 2);//返回與數組中所有條件都不匹配的文檔
 
 Query.Or(Query.EQ("name", "a"), Query.EQ("title", "t"));//滿足其中一個條件
 
 Query.Size("name", 2);//給定鍵的長度
 
 Query.Type("_id", BsonType.ObjectId );//給定鍵的類型
 
 Query.Where(BsonJavaScript);//執行JavaScript
 
 Query.Matches("Title",str);//模糊查詢 相當於sql中like -- str可包含正則表達式

 

其他參考如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MongoDB.Bson;
using MongoDB.Driver;

namespace mongodb
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //  MongoDB連接串,以[mongodb: // ]開頭。這里,我們連接的是本機的服務 
            //*    
            string connectionString = "mongodb://localhost";
            //  連接到一個MongoServer上 
            MongoServer server = MongoServer.Create(connectionString);
            //  -------------------------------------------------------------------------
            //  打開數據庫testdb 
            MongoDatabase db = server.GetDatabase("test");
            //  獲取集合employees 
            MongoCollection collection = db.GetCollection("user");

            //  -------------------------------------------------------------------------
            //  查詢上面那個剛剛插進去的數據,就這格式了,看看QueryDocument的文檔吧 
            var query = new QueryDocument();

            //query.Add("name", "zs");

            BsonDocument b = new BsonDocument();
            //b.Add("$gt", 30);
            b.Add("$lt", 30);
            query.Add("age", b);
            
            MongoCursor cur = collection.FindAs<BsonDocument>(query);

            //  遍歷結果 
            foreach (BsonDocument emp in cur)
            {
                //  BsonValue有兩種取值方式,下面兩個都用了一個是AsXXX,一個是ToXXX 
                var name = emp["name"].AsString;var age = 0.0;
                if (emp.Contains("age"))
                {
                    age = emp["age"].AsDouble;
                }
                Console.WriteLine(" name:{0},age:{1}", name, age);
            } //*/
        }
    }
}

 

 

 

 

/*---------------------------------------------            
* sql : SELECT * FROM table WHERE ConfigID > 5 AND ObjID = 1           
*---------------------------------------------             
*/            
QueryDocument query = new QueryDocument();            
BsonDocument b = new BsonDocument();            
b.Add("$gt", 5);                       
query.Add("ConfigID", b);
query.Add("ObjID", 1);
MongoCursor<Person> m = mongoCollection.FindAs<Person>(query);
 
/*---------------------------------------------            
* sql : SELECT * FROM table WHERE ConfigID > 5 AND ConfigID < 10           
*---------------------------------------------             
*/            
QueryDocument query = new QueryDocument();            
BsonDocument b = new BsonDocument();            
b.Add("$gt", 5);   
b.Add("$lt", 10);                 
query.Add("ConfigID", b);
MongoCursor<Person> m = mongoCollection.FindAs<Person>(query);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM