如何在MongoDB和Java中在返回语句中检索特定字段而不使用循环?

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

How can I retrieve specifc field without using loop in a return statement with MongoDB and Java?

问题

  1. @Override
  2. public User get(Object userId) {
  3. FindIterable<User> userTbl = database.getCollection("User", User.class).find();
  4. for (User doc : userTbl) {
  5. String id = doc.getId().toHexString();
  6. System.out.println("_id = " + id);
  7. if (id.equals(userId)) {
  8. return doc;
  9. }
  10. }
  11. return null;
  12. }
英文:

I have below working code using for loop to iterate all the id in the user collection. Although this post could help me to this question, it returns specific value as well. I wonder how to get the same result without using it because I can't complete the return statement.

  1. @Override
  2. public User get(Object userId) {
  3. FindIterable&lt;User&gt; userTbl = database.getCollection(&quot;User&quot;, User.class).find();
  4. for (User doc : userTbl) {
  5. String id = doc.getId().toHexString();
  6. System.out.println(&quot;_id = &quot; + id);
  7. if (id.equals(userId)) {
  8. return doc;
  9. }
  10. }
  11. return null;
  12. }

答案1

得分: 1

  1. 好的如果你只想返回一个字段那就这样做
  2. @Override
  3. public String get(Object userId) {
  4. FindIterable<User> userTbl = database.getCollection("User", User.class).find();
  5. for (User doc : userTbl) {
  6. String id = doc.getId().toHexString();
  7. System.out.println("_id = " + id);
  8. if (id.equals(userId)) {
  9. return doc.getUser();
  10. }
  11. }
  12. return null;
  13. }
  14. 但是使用 MongoRepository 应该会更方便可以更轻松地访问你的数据
  15. Repository 示例类使用 Spring):
  16. @Repository
  17. public interface UserRepository extends MongoRepository<User, String> {
  18. }
  19. 以及你的 get() 方法
  20. @Autowired
  21. private UserRepository userRepository;
  22. @Override
  23. public Optional<String> get(String userId) {
  24. Optional<User> user = userRepository.findById(userId);
  25. if (user.isPresent()) {
  26. return Optional.of(user.get().getId());
  27. }
  28. return Optional.empty();
  29. }
英文:

Well, if you just want to return one field, just do so.

  1. @Override
  2. public String get(Object userId) {
  3. FindIterable&lt;User&gt; userTbl = database.getCollection(&quot;User&quot;, User.class).find();
  4. for (User doc : userTbl) {
  5. String id = doc.getId().toHexString();
  6. System.out.println(&quot;_id = &quot; + id);
  7. if (id.equals(userId)) {
  8. return doc.getUser();
  9. }
  10. }
  11. return null;
  12. }

But it should be easier to use a MongoRepository to get easier access to your data.

Repository example class (with Spring):

  1. @Repository
  2. public interface UserRepository extends MongoRepository&lt;User, String&gt; {
  3. }

And your get() method:

  1. @Autowired
  2. private UserRepository userRepository;
  3. @Override
  4. public Optional&lt;String&gt; get(String userId) {
  5. Optional&lt;User&gt; user = userRepository.findById(userId);
  6. if (user.isPresent()) {
  7. return Optional.of(user.get().getId());
  8. }
  9. return Optional.empty();
  10. }

huangapple
  • 本文由 发表于 2020年10月14日 13:23:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64347102.html
匿名

发表评论

匿名网友

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

确定