gpt4 book ai didi

java - 对 MongoDB 和 Pooling、Bson 语法和文档格式感到困惑

转载 作者:行者123 更新时间:2023-12-02 10:39:40 25 4
gpt4 key购买 nike

我刚刚开始使用 MongoDB(2 小时后),我对一些事情感到非常困惑。

首先,我了解到不需要 MongoDB 连接池,因为它已经为您做到了这一点。这是真的吗?

其次,我正在集合中创建配置文件,其中一些配置文件可能包含到期日期和密码等信息,有些则没有。是否可以将他们的文档设置为包含这些内容,并且仍然是同一集合的成员?

第三,我为每个文档使用 UUID,这是否是检索特定文档的方法:

public static Document getPlayer(String uuid) {
return players.find(Document.parse("{uuid : " + uuid + "}")).first();
}

语法正确吗?我目前无法运行代码,因为我没有 MongoDB 服务器。

感谢您的帮助..我目前正在阅读文档并了解越来越多的内容。

最佳答案

Firstly, I read that MongoDB connection pooling isn't needed as it already does that for you. Is this true?

每当您创建mongoClient时有一个与之关联的连接池。您基本上不需要自己处理这些连接,而是在整个应用程序中使用单个客户端对象。您可能想看看connection文档在这里。

Secondly, I am creating profiles in a collection, the some of the profiles may have information like expiry dates, and passwords, some do not. Is it possible to just set their document to contain these and still be a member of the same collection?

这在 MongoDB 中绝对是可能的。它允许您将多态数据存储在单个集合中。

Thirdly, I'm using the UUID for each document, would this be the way to retrieve a specific document.

看一下 this doc 中完成的读取操作。

示例:

collection.find(
new Document("stars", new Document("$gte", 2)
.append("$lt", 5))
.append("categories", "Bakery")).forEach(printBlock);

关于java - 对 MongoDB 和 Pooling、Bson 语法和文档格式感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53016905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com