MongoDB学习与BUG解答

时间:2023-12-09 15:11:01

简单介绍:

MongoDb也是NoSQL中的一种,并且是应用比较火的一门解决高效处理数据的技术。

网上说它是介于关系数据库 和非关系数据库之间的产品,它是非关系数据库中最丰富的,最像关系数据的。

Q:那么为何这么说呢?A:因为它具有关系数据库的很多类型,但是它毕竟是以键值对存储的,所以不能撑的上关系型数据库。

MongoDb的安装看博客:http://www.cnblogs.com/mecity/archive/2011/06/11/2078527.html,这里我就不Copy了

然后说下MongoDb在VS中的使用。

首先呢,先引用两个驱动(MongoDB.Bson.dll,MongoDB.Driver.dll):可以用NuGet程序包管理器安装(方便快捷,推荐使用)。其实,就是下载下来,直接引入。

MongoDb C#驱动下载:http://www.cnblogs.com/zhwl/p/3421034.html

然后开始建立项目了:

简要项目文件截图:

MongoDB学习与BUG解答

App.config代码:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key ="MongoServerSettings" value="mongodb://127.0.0.1:27017"/>//这里定义IP和端口号
</appSettings>
</configuration>

Customer.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MongoDbDemo
{
public class Customer
{
public Object _id { get; set; }
public int CusId { get; set; }
public string Name { get; set; }
public DateTime Subtime { get; set; }
public string Demo { get; set; }
public string Shit { get; set; }
public string Demo2 { get; set; }
}
}

Program.cs代码:

using MongoDB.Driver;
using MongoDB.Driver.Builders;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
namespace MongoDbDemo
{
class Program
{
static void Main(string[] args)
{
string connStr = ConfigurationManager.AppSettings["MongoServerSettings"];//获取链接字符串
MongoServer server = MongoServer.Create(connStr);//创建mongodb服务对应的对象
MongoDatabase db = server.GetDatabase("Inferno1");//获取数据库,如果没有,会自动创建一个
var collectionName = typeof(Customer).Name;//指定集合的名字
var collection = db.GetCollection<Customer>(collectionName);//获取集合,如果集合不存在就直接创建一个
//添加实体
collection.RemoveAll();//清空所有数据
//循环添加100条数据
for (int i = ; i < ; i++)
{
Customer customer = new Customer();//创建实体
customer._id=i;
customer.CusId = i;
customer.Name = "shit" + i;
customer.Subtime = DateTime.Now;
customer.Demo = "ddd";
if (i == )
{
customer.Demo = "sssss";
}
customer.Shit = DateTime.Now.ToString(); collection.Insert(customer);//将数据插入到集合里面去
}
collection.Remove(Query.GT("CusId", 50));//根据条件删除数据,删除GusId大于50的数据
Console.WriteLine(collection.Count());//打印有多少条数据
// Console.ReadKey();
// collection.FindAll();//查看所有的数据
var cus = collection.FindOneAs<Customer>(Query.EQ("CusId", ));//根据条件查找CusId为1的对象
if (cus != null) {
cus.Demo2 = "hhhhhhhhhhhhhhhhh";
collection.Save(cus);
} }
}
}

在编写代码时,出现了以下BUG,当然上面的代码没BUG,那是修改后的代码,看BUG

MongoDB学习与BUG解答

这个问题弄的我很纠结,刚开始怀疑是驱动的问题,然后我重新引用驱动,用以上引用驱动的两种方法都试了,并且还找了别的驱动下载也试了。还是不行。

后来问群里说是MongoDB.Driver.dll驱动路径错了,但我检查了,并没错。

哎,其实遇到这种情况还是要根据实际弹出的指示出发,将错误信息一个一个验证。徒劳徒劳,就是部根据BUG而找BUG就是徒劳。

然后我 点击 将异常信息复制到剪切板 错误信息显示未识别的 _id,很郁闷哪里有这个属性啊?看了看数据库,它自动给添加了一个_id属性。

这是我尝试给Customer.cs文件添加这个属性 ,根据数据库中显示是object类型,所以我们也就添加object类型。

public object _id{get;set;}

添加后BUG就完美解决了~

总结:遇到BUG要根据错误详细信息入手,无故伸冤,最终也是爱莫能助!