MongoDB的:在ASP.NET MVC应用程序连接 [英] MongoDB: connecting with ASP.NET MVC application

查看:187
本文介绍了MongoDB的:在ASP.NET MVC应用程序连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个ASP.NET MVC应用5,和我收留了它在IIS 7我现​​在想开始使用MongoDB数据库与此应用程序。我已经得到了一个MongoDB的Windows服务运行,但我现在卡住作为下一个步骤是什么。

即:

我)如何在我的ASP.NET MVC应用程序是什么数据库的名称是指定,并在它的存储目录?

二)如何在MongoDB中创建这个数据库?

在我的Web.config文件中,我有以下行:

 <&是connectionStrings GT;
    <添加名称=蒙戈的connectionString =的MongoDB:// localhost的/>
< /&是connectionStrings GT;

 <&的appSettings GT;
    <添加键=DBNAMEVALUE =mydb的/>
    <添加键=MONGOHQ_URL值=的MongoDB:// localhost的/>
< /的appSettings>

所以,我现在该如何去建立一个名为数据库 MYDB ,使得它在MongoDB的Windows服务运行?


解决方案

  

我)如何在我的ASP.NET MVC应用程序是什么数据库的名称是指定,并在它的存储目录?


保存它的目录由您的mongod实例,而不是你的MVC应用程序指定。当您启动的mongod实例,你应该是这样的:

  mongod.exe --dbpath [您的数据库存储]

要了解更多关于mongod.exe参数,看看这里。结果
你的数据库的名称在连接字符串中指定。这将是这样的:

 的mongodb://本地主机/ DBNAME [选项]

您可以找到整个指令这里 。结果
但是,我不知道我是谁发现的棘手的C#驱动API类唯一的一个。要初始化MongoClient(顶级数据库对象),大多数人会去用最简单的方式:

  MongoClient客户端=新MongoClient(co​​nnStr);

这种方式,你将永远不会得到数据库名。为了得到它:

  MongoUrl URL =新MongoUrl(connStr);
MongoClient客户端=新MongoClient(URL);
VAR DBNAME = url.DatabaseName // retrive数据库名称
变种DB = client.GetServer()GetDatabase(数据库)。

这种方式可以存储数据库名称与连接字符串。这似乎是对我好。但是,你当然可以使用另一个appSetting存储数据库名称。


  

二)如何在MongoDB中创建这个数据库?


您不必。当您将数据插入到数据库中的第一次,mongod的将创建数据库为您,以及集合。虽然后来你可能会发现它有用建立索引上的一些藏品的。

I have created an ASP.NET MVC 5 application, and I am hosting it on IIS 7. I would now like to start using a MongoDB database with this application. I have managed to get a MongoDB Windows Service running, but I am now stuck as to what the next step is.

Namely:

i) How do I specify in my ASP.NET MVC application what the name of the database is, and the directory where it is stored?

ii) How do I create this database in MongoDB?

In my Web.config file, I have the following lines:

<connectionStrings>
    <add name="Mongo" connectionString="mongodb://localhost" />
</connectionStrings>

and:

<appSettings>
    <add key="DbName" value="mydb" />
    <add key="MONGOHQ_URL" value="mongodb://localhost" />
</appSettings>

So how do I now go about creating a database called mydb such that it is running in the MongoDB Windows Service?

解决方案

i) How do I specify in my ASP.NET MVC application what the name of the database is, and the directory where it is stored?

The directory where it is stored is specified by your mongod instance, not your MVC application. When you start your mongod instance, you should have something like:

mongod.exe --dbpath [where your db is stored]

To know more about mongod.exe parameters, have a look at here.
The name of your database is specified in the connection string. It would be something like:

mongodb://localhost/dbName?[options]

You can find the whole instruction here.
However, I don't know if I'm the only one who finds the C# driver API sort of tricky. To initialize a MongoClient (top level database object), most people would go with the most simple way:

MongoClient client = new MongoClient(connStr);

This way you'll never get the database name. To get it:

MongoUrl url = new MongoUrl(connStr);
MongoClient client = new MongoClient(url);
var dbName = url.DatabaseName  // retrive database name
var db = client.GetServer().GetDatabase(dbName);

This way you can store database name with connection string. Which seems to be good to me. But you can of course use another appSetting to store the db name.

ii) How do I create this database in MongoDB?

You don't have to. When you insert data into the database for the first time, mongod will create database for you, as well as collections. Although later on you may find it useful to build indexes on some of the collections.

这篇关于MongoDB的:在ASP.NET MVC应用程序连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆