MongoDB学习笔记(四)用MongoDB的文档结构描述数据关系(4)
来源:未知 责任编辑:责任编辑 发表时间:2013-11-15 19:51 点击:次
}
}
public class UserBLL
{
public string connectionString = "mongodb://localhost";
public string databaseName = "myDatabase";
public string collectionName = "UserInfo";
private Mongo mongo;
private MongoDatabase mongoDatabase;
private MongoCollection<Document> mongoCollection;
public UserBLL()
{
mongo = new Mongo(connectionString);
mongoDatabase = mongo.GetDatabase(databaseName) as MongoDatabase;
mongoCollection = mongoDatabase.GetCollection<Document>(collectionName) as MongoCollection<Document>;
mongo.Connect();
}
~UserBLL()
{
mongo.Disconnect();
}
/// <summary>
/// 插入一些数据
/// </summary>
public void InsertSomeData()
{
Document userInfo1 = new Document();
userInfo1["UserId"] = "1001";
userInfo1["UserName"] = "张三";
userInfo1["PassWord"] = "123456";
mongoCollection.Save(userInfo1);
Document userInfo2 = new Document();
userInfo2["UserId"] = "1002";
userInfo2["UserName"] = "李四";
userInfo2["PassWord"] = "123456";
//子文档
var userInfo2Detail = new Document();
userInfo2Detail["Address"] = "湖北";
userInfo2Detail["Age"] = 20;
userInfo2Detail["Email"] = "lisi@163.com";
userInfo2["Detail"] = userInfo2Detail;
mongoCollection.Save(userInfo2);
Document userInfo3 = new Document();
userInfo3["UserId"] = "1003";
userInfo3["UserName"] = "王五";
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>