using System.Collections.Generic;
using System.Linq;
using MongoDB.Driver;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
namespace DB
{
public class User
{
public ObjectId _id;
public string name;
public string phoneNumber;
public string password="123456";
}
/// <summary>
/// Mongo帮助类
/// </summary>
public class MongoHelper<T>
{
private string connStr = "";//服务器网址
private string dbName = "";//数据库名称
private IMongoClient client;//连接客户端
private IMongoDatabase db;//连接数据库
private string collName;//集合名称
public MongoHelper()
{
}
public MongoHelper(string connStr, string dbName, string collName)
{
this.connStr = connStr;
this.dbName = dbName;
this.collName = collName;
this.Init();
}
/// <summary>
/// 初始化连接客户端
/// </summary>
private void Init()
{
if (client == null)
{
client = new MongoClient(this.connStr);
}
if (db == null)
{
db = client.GetDatabase(this.dbName);
}
}
/// <summary>
/// 插入对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
public void Insert(T obj)
{
IMongoCollection<T> collention = db.GetCollection<T>(collName);
collention.InsertOneAsync(obj);
}
/// <summary>
/// 插入对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
public void Insert(T obj, string collName)
{
IMongoCollection<T> collention = db.GetCollection<T>(collName);
collention.InsertOneAsync(obj);
}
/// <summary>
/// 字典形式插入
/// </summary>
/// <param name="dicInfo"></param>
public void Insert(Dictionary<string, string> dicInfo)
{
var collection = db.GetCollection<BsonDocument>(collName);
var document = new BsonDocument(dicInfo);
collection.InsertOne(document);
}
/// <summary>
/// 无条件查询,即返回全部
/// </summary>
/// <returns></returns>
public List<T> Query()
{
var collection = db.GetCollection<BsonDocument>(collName);
var rest = collection.Find(Builders<BsonDocument>.Filter.Empty);
return rest.As<T>().ToList();
}
/// <summary>
/// 按名称进行查询
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public List<T> Query(object name)
{
var collection = db.GetCollection<T>(collName);
var rest = collection.Find(Builders<T>.Filter.Eq("name", name));
return rest.As<T>().ToList();
}
/ <summary>
/ 只查询一条
/ </summary>
/ <param name="Id"></param>
/ <returns></returns>
//public string QueryOne(string Id)
//{
// var collection = db.GetCollection<T>(collName);
// var rest = collection.Find(Builders<T>.Filter.Eq("Id", new ObjectId(Id))).Limit(1);
// T t = rest.As<T>().ToList()[0];
// JavaScriptSerializer jserializer = new JavaScriptSerializer();
// string strJson = jserializer.Serialize(t);
// return strJson;
//}
}
}
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DB;
namespace MogonDBTest
{
internal class Program
{
static void Main(string[] args)
{
//1.连接字符串(mongodb数据库默认端口为:27017)
string connStr = "mongodb://127.0.0.1:27017";
string dataName = "SoftBody";
string collectionName = "Users";
MongoHelper<User> mongoHelper = new MongoHelper<User>(connStr,dataName,collectionName);
mongoHelper.Insert(new User(){name = "json",phoneNumber = "18366183666"});
2.创建连接MongoClient实例实际上代表了一个到数据库的连接池,即使有多个线程,也只需要一个MongoClient类的实例
//var client = new MongoClient(connStr);
3.使用client的GetDatabase方法获取数据库,即使该数据库不存在,也会自动创建;
//IMongoDatabase db = client.GetDatabase("SoftBody");
4.获取数据集 collection;BsonDocument是在数据没有预先定义好的情况下使用的。
//IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("Users");
//5.插入一条数据;
var document = new BsonDocument { { "id", 3}, { "name", "json" } };
collection.InsertOne(document);
6.查询数据1
//var res = collection.Find(new BsonDocument()).ToList(); //查询整个数据集
//foreach (var item in res)
//{
// var value = item["_id"];
// //Console.WriteLine(item);
// Console.WriteLine(value);
//}
Console.ReadKey();
}
}
}