使用正文内容更新 mongo 对象

Update mongo object using body content

所以我知道我需要通过 uuid 查询用户,然后更新用户,但我在如何执行此操作时遇到了麻烦,这是我当前的代码。 我目前正在使用吗啡和火花框架。

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";
}

}

我一直在寻找,但没有人只使用吗啡mongodb,我试过但失败了。

用户路线:

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);
}
}

用户class: 只是一个 class 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;
    }
}

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

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);
  }
}