知识学堂 > 课程 > MongoDB数据库操作

MongoDB数据库操作

发布日期:2019/5/17 来源:聚恒【返回】

MongoDB,数据库操作

导入到bin文件夹DLL


添加访问类库MongoDB.rar  

Query语法

 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 MongoDB;

using MongoDB.Driver.Builders;

using Model;

using MongoDB.Bson;

 

public MongoDbHelper mh = new MongoDbHelper("1000");


Id查询

Model.Resource  Resource  = new Model.Resource ();

Resource = mh.FindOne<Model.Resource>(MongoDB.Driver.Builders.Query.And(MongoDB.Driver.Builders.Query.EQ("_id", new MongoDB.Bson.ObjectId(item.ResourceID))));

模糊查询

Resource_list = mh.Find<Resource>(Query.And(Query.Matches("Title", title)));

一般查询

Resource_list = mh.Find<Resource>(Query.And(Query.EQ("Type", id)));


实体类


using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson;

namespace Model
{
    /// <summary>
    /// 功能:会员资料
    /// 邮件:314220380@qq.com
    /// 作者:夜未央
    /// </summary>
    [Serializable]
    [BsonIgnoreExtraElements]
    public class Collection
    {
        [BsonId]
        public ObjectId _id { get; set; }
        #region Model
        public string ResourceID;
        public string ResourceTitle;
        public int ResourceType;
        public string Intro;
        public string Uploader;
        public string Company;
        public int Type;
        public int? Money;
        public DateTime? scsj;
        public int? UserID;
        #endregion Model
    }
}
一般使用方法 需要自己修改


using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.Mvc;  
using MongoDB;  
using MongoDbDemo;  
using MongoDB.Driver;  
using MongoDB.Bson;  
using DAL.Entity;  
using DAL;  
using MongoDB.Driver.Builders;  
using System.Diagnostics;  
  
namespace MongoDbDemo.Controllers  
{  
    public class HomeController : Controller  
    {  
        //  
        // GET: /Home/  
  
        public ActionResult Index()  
        {  
            return Content("MongoDbDemo");  
        }  
 
        #region INSERT  
        /// <summary>  
        /// 添加用户  
        /// </summary>  
        /// <returns></returns>  
        public ActionResult Ruser()  
        {  
            try  
            {  
                string name = Request["name"];  
                string age = Request["age"];  
                User user1 = new User { Uname = name, Age = age };  
                MongoDbHelper mh = new MongoDbHelper("127.0.0.1", "1000");  
                if (mh.Insert<User>(user1))  
                {  
                    return Json(new { success = "true" });  
                }  
            }  
            catch (Exception)  
            {  
                return Json(new { success = "filled" });  
                throw;  
            }  
            return Json(new { success = "filled" });  
        }  
        #endregion  
 
        #region SELECT  
        /// <summary>  
        /// 查询一个集合中的所有数据  
        /// </summary>  
        /// <returns></returns>  
        public ActionResult Seluser()  
        {             
            MongoDbHelper mh = new MongoDbHelper("127.0.0.1", "1000");  
            List<User> users = mh.FindAll<User>();  
            return Json(new { success="true"});  
        }  
  
        /// <summary>  
        /// 按条件查询一条数据  
        /// </summary>  
        /// <returns></returns>  
        public ActionResult SelOne()  
        {  
            string name = Request["name"];  
            string age=Request["age"];  
            MongoDbHelper mh = new MongoDbHelper("127.0.0.1", "1000");  
            User users = mh.FindOne<User>(Query.And(Query.EQ("Uname", name), Query.EQ("Age", age)));  
            return Json(new {success="true" ,users=users});  
        }  
  
        /// <summary>  
        /// 分页查询  
        /// </summary>  
        /// <returns></returns>  
        public ActionResult SelPage()   
        {  
            int pageindex=int.Parse(Request["pageindex"]);//页索引  
            int pagesize = int.Parse(Request["pagesize"]);//行数  
            MongoDbHelper mh = new MongoDbHelper("127.0.0.1", "1000");  
            Stopwatch sw1 = new Stopwatch();  
            sw1.Start();  
            List<User> users = mh.Find<User>(null,pageindex,pagesize,null);  
            return Json(new {success="true",users=users });  
        }  
        #endregion  
 
        #region UPDATE  
        /// <summary>  
        /// 修改信息  
        /// </summary>  
        /// <returns></returns>  
        public ActionResult upduser()  
        {  
            MongoDbHelper mh = new MongoDbHelper("127.0.0.1", "1000");  
            if (mh.Update<User>(Query.EQ("Uname", "阿斯达"), Update.Set("Uname", "阿斯达s"), "User"))  
            {  
                return Json(new { success = "true" });  
            }  
            else  
            {  
                return Json(new { success = "fales" });  
            }  
  
        }  
        #endregion  
 
        #region DELETE  
        /// <summary>  
        /// 删除消息  
        /// </summary>  
        /// <returns></returns>  
        public ActionResult deluser()   
        {  
            MongoDbHelper mh = new MongoDbHelper("127.0.0.1", "1000");  
            if (mh.Remove<User>(Query.EQ("Uname", "阿斯达s")))  
            {  
                return Json(new { success = "true" });  
            }  
            else {  
                return Json(new { success = "fales" });  
            }  
        }  
        #endregion  
    }  
}

如果用户名和密码访问需要加上: