英文:
How can I make a Java app that connects to a MongoDB Atlas Cluster?
问题
我正在尝试制作一个连接到MongoDB Atlas集群的Java应用程序。我目前在使用Eclipse作为我的集成开发环境。
根据我所了解的,我应该使用Maven项目来安装依赖项,但由于我更喜欢下载JAR文件并在项目中使用它们,所以我已经从https://repo1.maven.org/maven2/org/mongodb/下载了3个文件(这些文件是在向MongoDB添加依赖项时Maven下载的):mongodb-driver-sync-4.1.1;mongodb-drive-core-4.1.1和bson-4.1.1,然后将这些文件添加到了我的Java项目中。使用这种方法,像MongoDatabase和MongoClient这样的东西都能正常工作,但问题是,每当我尝试使用MongoCollection<Document> name = database.getCollection("collection")
时,我会收到“无法将Document解析为类型”的错误,就好像IDE无法识别它一样。值得注意的是,当我尝试使用MongoClientURI时,我得到了相同的错误。
我已经尝试创建一个带有之前提到的依赖关系的Maven项目,它可以正常工作,我不会从Document或MongoClientURI中获得错误,但是当我在Maven创建的“main”文件夹中创建一个类并尝试执行它时,我会收到“启动错误,编辑器不包含主类型”的错误消息,当然,这个类包含主方法。
如果我尝试将类移动到src/main/java(顺便说一句,这是它最初创建的位置),我会得到与使用Java项目时相同的错误。
(请注意,MongoDB是Java项目,而Mongo是Maven项目)
我猜我可能做错了什么,所以我想我会坚持使用Java项目(还考虑到我想使用Window Builder,但似乎无法使其与Maven一起工作)。
我知道内容比较多,但你们能指导我正确的方向吗?提前谢谢。
英文:
I'm trying to make a Java app that connects to a MongoDB Atlas cluster. I'm currently using Eclipse as my IDE.
From what I've read I'm supposed to use a Maven project to install dependencies, but since I prefer downloading JAR files and using them in my project I've downloaded 3 files (which are the ones that Maven downloads when you add the dependency for MongoDB): mongodb-driver-sync-4.1.1; mongodb-drive-core-4.1.1 and bson-4.1.1 from https://repo1.maven.org/maven2/org/mongodb/ and then added those files to my Java project. Using this method things like MongoDatabase and MongoClient work fine, but the problem is that whenever I try to use MongoCollection <Document> name = database.getCollection("collection")
I get the error "Document cannot be resolved to a type", as if the IDE wouldn't recognize it. It's also worth noting that when trying to use MongoClientURI I get the same error.
I've tried creating a Maven project with the dependency mentioned before and it works just fine, I don't get errors from Document nor MongoClientURI, but when I create a class in the "main" folder that Maven creates and try to execute it I get a message error saying "launch error editor does not contain a main type", which of course, the class contains.
If I try to move the class to src/main/java (which by the way it's where it's first created) I get the same error as when using the Java project
(Note that MongoDB is the Java project and Mongo is the Maven project)
Guess I'm doing something wrong, so I guess I'll stick with a Java project (also given the fact that I want to use Window Builder and I can't seem to make it work with Maven).
I know it's a lot, but could you guys point me in the right direction? Thanks in advance.
答案1
得分: 0
好的,显然我没有导入org.bson.*
,其中包含了Document类...所以这个问题解决了。
英文:
Okay, apparently I wasn't importing org.bson.*
, which contains the Document class... So that's solved
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论