MongoDB学习笔记(四)用MongoDB的文档结构描述数据关系(5)
来源:未知 责任编辑:责任编辑 发表时间:2013-11-15 19:51 点击:次
userInfo3["PassWord"] = "123456";
var userInfo3Detail = new Document();
userInfo3Detail["Address"] = "广东";
userInfo3Detail["Age"] = 20;
userInfo3Detail["Email"] = "wangwu@163.com";
userInfo3["Detail"] = userInfo3Detail;
mongoCollection.Save(userInfo3);
Document userInfo4 = new Document();
userInfo4["UserId"] = "1004";
userInfo4["UserName"] = "赵六";
userInfo4["PassWord"] = "123456";
var userInfo4Detail = new Document();
userInfo4Detail["Address"] = "湖北";
userInfo4["Detail"] = userInfo4Detail;
mongoCollection.Save(userInfo4);
}
/// <summary>
/// 查询详细资料地址为湖北的用户信息
/// </summary>
public IEnumerable<Document> Select()
{
return mongoCollection.Find(new Document { { "Detail.Address", "湖北" } }).Documents; www.2cto.com
}
/// <summary>
/// 删除全部用户信息
/// </summary>
public void DeleteAll()
{
mongoCollection.Remove(new Document { });
}
}
最后,我们通过这段代码输出全部用户资料信息的BJSON格式:
/// <summary>
/// 打印数据BJSON
/// </summary>
public void PrintBJSON()
{
string BJSON = string.Empty;
foreach (var documet in mongoCollection.FindAll().Documents)
{
BJSON += documet.ToString();
}
Console.WriteLine(BJSON);
}
结果如下:
{ "UserId": "1001", "UserName": "张三", "PassWord": "123456", "_id": "4d80ec1ab8a4731338000001" }
{ "UserId": "1002", "UserName": "李四", "PassWord": "123456", "Detail": { "Address": "湖北", "Age": 20, "Email": "lisi@163.com" }, "_id": "4d80ec1ab8a4731338000002" }
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>