使用正文内容更新mongo对象

时间:2017-12-12 19:12:15

标签: java mongodb morphia spark-framework

所以我知道我需要通过uuid查询用户,然后更新用户但是我遇到了如何做的麻烦,这是我目前的代码。 我目前正在使用morphia和spark-framework。

import eu.unionmc.api.model.User;
import eu.unionmc.api.service.dao.UserDAO;
import eu.unionmc.api.utils.DatabaseHandler;

import java.util.List;

public class UserService {

private DatabaseHandler dbHandler = new DatabaseHandler();
private UserDAO userDAO = dbHandler.getUserDAO();

public String addUser(User user){
    userDAO.save(user);
    return "add user";
}

public List<User> getAllUsers(){
    return userDAO.find().asList();
}

public User getByUsername(String username){
    return userDAO.findOne("username", username);
}

public User getByUUID(String uuid){
    return userDAO.findOne("uuid", uuid);
}

public String update(String uuid, String body){
    return "Not found";
}

}

我一直在寻找,但没有人使用只有miaodb的morphiab,我试过但失败了。

用户路线:

public class UserRoutes implements ExportRoute {

private UserService userService = new ServiceFactory().getUserService();

@Override
public void export() {
    Gson gson = new Gson();
    String apiPath = "/api/";

    /*
     * Return all users in a JSON format
     */
    get(apiPath + "users", (req, res) -> {
        res.type("application/json");
        return userService.getAllUsers();
    }, gson::toJson);

    /*
     * Return a user match with params username
     */
    get(apiPath + "users/:username", (req, res) -> {
        res.type("application/json");
        User user = userService.getByUsername(req.params("username"));

        if(user != null){
            return user;
        }else {
            return "User not found";
        }

    }, gson::toJson);

    get(apiPath + "users/uuid/:uuid", (req, res) -> {
        res.type("application/json");
        User user = userService.getByUUID(req.params("uuid"));

        if(user != null){
            return user;
        }else {
            return "User not found";
        }

    }, gson::toJson);


    /*
     * Create a new user using a json body format
     */
    post(apiPath + "users", (req, res) -> {
        res.type("application/json");
        User user = gson.fromJson(req.body(), User.class);
        return userService.addUser(user);
    }, gson::toJson);

    //Update existing user by uuid
    put(apiPath + "users/:uuid", (req, res) -> {
        res.type("application/json");
        return "Do something to update";
    }, gson::toJson);
}
}

用户类: 只是一个类POJO

   package eu.unionmc.api.model;

    import org.bson.types.ObjectId;
    import org.mongodb.morphia.annotations.Entity;
    import org.mongodb.morphia.annotations.Id;
    import org.mongodb.morphia.annotations.IndexOptions;
    import org.mongodb.morphia.annotations.Indexed;

    import java.util.UUID;

    @Entity(value = "users", noClassnameStored = true)
    public class User {

    @Id
    private ObjectId id;

    @Indexed(options = @IndexOptions(unique = true))
    private String uuid;

    private String username;
    private long coins, tokens;

    public User() {
    }

    public ObjectId getId() {
        return id;
    }

    public String getUUID() {
        return uuid;
    }

    public void setUUID(UUID uuid) {
        this.uuid = uuid.toString();
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public long getCoins() {
        return coins;
    }

    public void setCoins(long coins) {
        this.coins = coins;
    }

    public long getTokens() {
        return tokens;
    }

    public void setTokens(long tokens) {
        this.tokens = tokens;
    }
}

1 个答案:

答案 0 :(得分:0)

在您的DAO中,您需要访问morphia的数据存储对象,然后您可以执行以下查询:

public class UserDao{

// ... your configs, you need to have a Datastore object available here

public void findByUuidAndUpdate(String uuid, String newValue){
   query = datastore.createQuery(User.class).field("uuid").equal(uuid);
   ops = datastore.createUpdateOperations(User.class).set("fieldToUpdate", 
newValue);

    datastore.update(query, ops);
  }
}