英文:
Return the Specified Fields and the _id Field Only in java
问题
UPDATE: Spuggiehawk建议在他的答案中修复include
关键字问题,并提出了一种除了projections
之外获取_id
的替代方法。然而,我仍然在从我的服务类调用此方法时遇到问题,该方法用于编辑用户详细信息,我必须承认我在正确操作方面的知识有限。
@Override
public User get(Object userId) {
FindIterable<User> userTbl = database.getCollection("User", User.class).find();
User user = new User();
for (User doc : userTbl) {
String oid = doc.getId().toHexString();
System.out.println("_id = " + oid);
return user;
}
return null;
}
在Service类中
public void editUser() {
String userId = request.getParameter("id");
User user = userDAO.get(userId);
System.out.println(user.getFullName());
}
英文:
UPDATE: Spuggiehawk advised in his answer to fix the include
keyword issue, and also suggest an alternative way to get the _id
other than projections
. However, I still have trouble to call this method from my service class, which edit the user detail, that I must admit I have limited knowledge to make it right.
@Override
public User get(Object userId) {
FindIterable<User> userTbl = database.getCollection("User", User.class).find();
User user = new User();
for (User doc : userTbl) {
String oid = doc.getId().toHexString();
System.out.println("_id = " + oid);
return user;
}
return null;
}
In the Service class
public void editUser() {
String userId = request.getParameter("id");
User user = userDAO.get(userId);
System.out.println(user.getFullName());
}
答案1
得分: 1
你只需要获取对象ID时,无需使用投影。在循环中获得所需语法如下:
FindIterable<Document> userTbl = db.getCollection("User").find();
for (Document doc : userTbl) {
String id = doc.getObjectId("_id").toString();
System.out.println("_id = " + id);
}
根据这个id值进行需要的操作。
至于您对于使用include的用法,如果您发现需要使用它,那么您需要进行静态导入。在Eclipse中,如果您将鼠标悬停在关键字上,应该会出现选项:
(图片已省略)
如果Eclipse没有显示这个选项,您可能需要在Eclipse配置中的“窗口” -> “首选项” -> “Java” -> “编辑器” -> “内容辅助” -> “收藏夹”下添加引用:
(图片已省略)
重要的部分位于您的代码顶部,应该包括:
import static com.mongodb.client.model.Projections.include;
您还可以在过滤器中使用这个方法,例如:
Bson filter = eq("email", "email.com");
db.getCollection("User").find(filter);
最后,如果您只想在find()中获取第一个匹配的记录,可以使用:
Document result = db.getCollection("User").find(filter).first();
英文:
You don't need to use projection if you just want the object ID. The syntax you want to get that (in a loop) is:
FindIterable<Document> userTbl = db.getCollection("User").find();
for (Document doc: userTbl2)
{
String id = doc.getObjectId("_id").toString();
System.out.println("_id = " + id);
}
Do what you need to with that id value.
As far as your use of include is concerned, if you do find a situation where you need that, then you need the static import. Eclipse should give you the option if you hover over the keyword:
If Eclipse doesn't show that, you might need to add the references in your Eclipse configuration under Window -> Preferences -> Java -> Editor -> Content Assist -> Favorites:
The important part is at the top of your code, it should include:
import static com.mongodb.client.model.Projections.include;
You'll find that useful for your filters too, eg.
Bson filter = eq("email", "email.com");
db.getCollection("User").find(filter);
Finally, if you only want to get the first matching record in your find(), use:
Document = db.getCollection("User").find(filter).first();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论