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

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

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

问题

@Override
public User get(Object userId) {

    FindIterable<User> userTbl = database.getCollection("User", User.class).find();
    for (User doc : userTbl) {

        String id = doc.getId().toHexString();
        System.out.println("_id = " + id);

        if (id.equals(userId)) {
            return doc;
        }
    }

    return null;
}
英文:

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.

@Override
public User get(Object userId) {

	FindIterable&lt;User&gt; userTbl = database.getCollection(&quot;User&quot;, User.class).find();
	for (User doc : userTbl) {

		String id = doc.getId().toHexString();
		System.out.println(&quot;_id = &quot; + id);

		if (id.equals(userId)) {
			return doc;
		}
	}

	return null;
}

答案1

得分: 1

好的如果你只想返回一个字段那就这样做

@Override
public String get(Object userId) {

    FindIterable<User> userTbl = database.getCollection("User", User.class).find();
    for (User doc : userTbl) {

        String id = doc.getId().toHexString();
        System.out.println("_id = " + id);

        if (id.equals(userId)) {
            return doc.getUser();
        }
    }

    return null;
}

但是使用 MongoRepository 应该会更方便可以更轻松地访问你的数据

Repository 示例类使用 Spring):

@Repository
public interface UserRepository extends MongoRepository<User, String> {
}

以及你的 get() 方法

@Autowired
private UserRepository userRepository;

@Override
public Optional<String> get(String userId) {

    Optional<User> user = userRepository.findById(userId);
    if (user.isPresent()) {
        return Optional.of(user.get().getId());
    }
    return Optional.empty();
}
英文:

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

@Override
public String get(Object userId) {

    FindIterable&lt;User&gt; userTbl = database.getCollection(&quot;User&quot;, User.class).find();
    for (User doc : userTbl) {

        String id = doc.getId().toHexString();
        System.out.println(&quot;_id = &quot; + id);

        if (id.equals(userId)) {
            return doc.getUser();
        }
    }

    return null;
}

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

Repository example class (with Spring):

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

And your get() method:

@Autowired
private UserRepository userRepository;

@Override
public Optional&lt;String&gt; get(String userId) {

    Optional&lt;User&gt; user = userRepository.findById(userId);
    if (user.isPresent()) {
        return Optional.of(user.get().getId());
    }
    return Optional.empty();
}

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:

确定