简体   繁体   English

如何使用Mongo DB Driver .NET设置mongo本地服务器

[英]how to set mongo local server using Mongo DB Driver .NET

I want to add an option to save data locally in my application using the mongo databse tools, I want to configure all the server information from within my application. 我想添加一个选项,以便使用mongo databse工具在我的应用程序中本地保存数据,我想从我的应用程序中配置所有服务器信息。 I have 2 questions. 我有两个问题。

  1. the following code is working only after manual setup of mongodb localhost database in this way: 以下代码仅在以这种方式手动设置mongodb localhost数据库后才有效:

在此处输入图片说明

but on A computer that didn't configure the database setting, the code will not work. 但是在未配置数据库设置的计算机上,该代码将不起作用。 my code is : 我的代码是:

public void createDB()
{
    MongoClient client = new MongoClient();
    var db = client.GetDatabase("TVDB");
    var coll = db.GetCollection<Media>("Movies");
    Media video = new Media("", "");
    video.Name = "split";
    coll.InsertOne(video);
}

this code works only after manual set the database like the picture above. 仅在手动设置数据库(如上图)后,此代码才有效。 without it I get in the last line A timeout exception. 没有它,我进入最后一行超时异常。 how can I configure it from my application to make it work (define Server) ? 如何从应用程序中配置它以使其正常运行(定义服务器)?

  1. Is the user will be must install MongoDB software on his PC, or the API Package is enough in order to use the database? 用户是否必须在PC上安装MongoDB软件,或者API包足以使用数据库? Many Thanks! 非常感谢!

By using that command you're not "configuring the database", you're running it. 通过使用该命令,您不是在“配置数据库”,而是在运行它。

If you don't want to manually run it, but want it to be always running, you should install it as a Windows Service as explained in How to run MongoDB as Windows service? 如果您不想手动运行它,而是希望它始终运行,则应按照如何将MongoDB作为Windows服务运行中的说明将其安装为Windows服务。 .

You need to install and/or run a MongoDB server in order to use it. 您需要安装和/或运行MongoDB服务器才能使用它。 Using the API alone is not enough, it's not like SQLite. 仅仅使用API​​是不够的,它不像SQLite。

您使用的代码将搜索本地mongodb。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM