返回指定字段和`_id`字段,仅使用Java。

huangapple go评论92阅读模式
英文:

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&lt;User&gt; userTbl = database.getCollection(&quot;User&quot;, User.class).find();

User user = new User();

for (User doc : userTbl) {
	
	String oid = doc.getId().toHexString();

	System.out.println(&quot;_id = &quot; + oid);	
		
		return user;
	}

	return null;
}

In the Service class

public void editUser() {
	
	String userId = request.getParameter(&quot;id&quot;);
	
	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&lt;Document&gt; userTbl = db.getCollection(&quot;User&quot;).find();
    for (Document doc: userTbl2)
    {
        String id = doc.getObjectId(&quot;_id&quot;).toString();
        System.out.println(&quot;_id = &quot; + 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:

返回指定字段和`_id`字段,仅使用Java。

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:

返回指定字段和`_id`字段,仅使用Java。

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(&quot;email&quot;, &quot;email.com&quot;);
db.getCollection(&quot;User&quot;).find(filter);

Finally, if you only want to get the first matching record in your find(), use:

Document = db.getCollection(&quot;User&quot;).find(filter).first();

huangapple
  • 本文由 发表于 2020年10月11日 20:22:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64303956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定