MongoDB Morphia 仅节省 1 位用户
MongoDB Morphia only saving 1 user
大家好,我在为 mongodb 使用吗啡时遇到了问题,这就是我正在创建的。
我为我的中心服务器创建了一个 spigot 插件,我使用 mongodb 和 morphia 将我的用户对象存储到我的集合中,这个对象只存储 1 个用户而不是将所有用户保存到集合中。
我的用户对象
@Entity(value = "clientdata", noClassnameStored = true)
public class ClientData {
@Id
public int id;
@Indexed(options = @IndexOptions(unique = true))
private String uuid;
@Indexed
private String lastName, username, lastLoginDate, ip;
@Indexed
private int level, exp, joins, coins, pearls;
@Property("hats")
private List<Integer> hatsOwned;
public ClientData(){
this.hatsOwned = new ArrayList<>();
if(this.hatsOwned.isEmpty()){
this.hatsOwned.add(0);
}
}
public int getId() {
return id;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public void setId(int id) {
this.id = id;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getExp() {
return exp;
}
public void setExp(int exp) {
this.exp = exp;
}
public int getJoins() {
return joins;
}
public void setJoins(int joins) {
this.joins = joins;
}
public void addJoins(int joins) {
this.joins += joins;
}
public int getCoins() {
return coins;
}
public void setCoins(int coins) {
this.coins = coins;
}
public int getPearls() {
return pearls;
}
public void setPearls(int pearls) {
this.pearls = pearls;
}
public List<Integer> getHatsOwned() {
return hatsOwned;
}
public void setHatsOwned(List<Integer> hatsOwned) {
this.hatsOwned = hatsOwned;
}
public void addNewHatOwned(int hatID){
this.hatsOwned.add(hatID);
}
public String getLastLoginDate() {
return lastLoginDate;
}
public void setLastLoginDate(String lastLoginDate) {
this.lastLoginDate = lastLoginDate;
}
}
我的 mongo 经理 class
public class MongoManager {
private static MongoManager ins = new MongoManager();
private MongoClient mc;
private Morphia morphia;
private Datastore datastore;
private ClientDAO userDAO;
public static MongoManager get() {
return ins;
}
public void init() {
ServerAddress addr = new ServerAddress("127.0.0.1", 27017);
List<MongoCredential> credentials = new ArrayList<>();
credentials.add(MongoCredential.createCredential("union", "admin", "union16".toCharArray()));
mc = new MongoClient(addr, credentials);
morphia = new Morphia();
morphia.map(ClientData.class);
datastore = morphia.createDatastore(mc, "admin");
datastore.ensureIndexes();
userDAO = new ClientDAO(ClientData.class, datastore);
}
public void disconnect(){
this.mc.close();
}
public ClientData getUserByPlayer(Player player) {
ClientData du = userDAO.findOne("uuid", player.getUniqueId().toString());
long time = System.currentTimeMillis();
if (du == null) {
du = new ClientData();
du.setUuid(player.getUniqueId().toString());
du.setCoins(0);
du.setExp(0);
du.setJoins(0);
du.setLastLoginDate(DateFormat.getTimeInstance().format(time));
du.setLevel(0);
du.setPearls(0);
du.setIp(player.getAddress().getAddress().toString());
du.setUsername(player.getDisplayName());
du.setLastName(player.getName());
du.setHatsOwned(null);
userDAO.save(du);
}
return du;
}
public void saveUser(ClientData user) {
userDAO.save(user);
}
public List<ClientData> getAllUsers() {
return userDAO.find().asList();
}
}
你不能插入多个,因为你有 duplicateKey: 你不能在没有自己设置唯一值的情况下使用原始类型的@id.
你这样定义你的 id:
@Id
public int id;
即使您从未为 id 设置任何值,原始类型也会被初始化为 0
,因此您最终会尝试使用相同的键插入多个文档。
解决方案:
您可以:
- 将您的
id
更改为 String:@Id String id;
- 将您的
id
更改为 ObjectId:@Id ObjectId id;
- 保留
@Id int id
并为其手动设置一个唯一值(例如 player.getUniqueId()
)。
前 2 个选项将起作用,因为它们不会被初始化并且将为空。 Mongo 将为您生成一个唯一的 ID。
大家好,我在为 mongodb 使用吗啡时遇到了问题,这就是我正在创建的。 我为我的中心服务器创建了一个 spigot 插件,我使用 mongodb 和 morphia 将我的用户对象存储到我的集合中,这个对象只存储 1 个用户而不是将所有用户保存到集合中。
我的用户对象
@Entity(value = "clientdata", noClassnameStored = true)
public class ClientData {
@Id
public int id;
@Indexed(options = @IndexOptions(unique = true))
private String uuid;
@Indexed
private String lastName, username, lastLoginDate, ip;
@Indexed
private int level, exp, joins, coins, pearls;
@Property("hats")
private List<Integer> hatsOwned;
public ClientData(){
this.hatsOwned = new ArrayList<>();
if(this.hatsOwned.isEmpty()){
this.hatsOwned.add(0);
}
}
public int getId() {
return id;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public void setId(int id) {
this.id = id;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getExp() {
return exp;
}
public void setExp(int exp) {
this.exp = exp;
}
public int getJoins() {
return joins;
}
public void setJoins(int joins) {
this.joins = joins;
}
public void addJoins(int joins) {
this.joins += joins;
}
public int getCoins() {
return coins;
}
public void setCoins(int coins) {
this.coins = coins;
}
public int getPearls() {
return pearls;
}
public void setPearls(int pearls) {
this.pearls = pearls;
}
public List<Integer> getHatsOwned() {
return hatsOwned;
}
public void setHatsOwned(List<Integer> hatsOwned) {
this.hatsOwned = hatsOwned;
}
public void addNewHatOwned(int hatID){
this.hatsOwned.add(hatID);
}
public String getLastLoginDate() {
return lastLoginDate;
}
public void setLastLoginDate(String lastLoginDate) {
this.lastLoginDate = lastLoginDate;
}
}
我的 mongo 经理 class
public class MongoManager {
private static MongoManager ins = new MongoManager();
private MongoClient mc;
private Morphia morphia;
private Datastore datastore;
private ClientDAO userDAO;
public static MongoManager get() {
return ins;
}
public void init() {
ServerAddress addr = new ServerAddress("127.0.0.1", 27017);
List<MongoCredential> credentials = new ArrayList<>();
credentials.add(MongoCredential.createCredential("union", "admin", "union16".toCharArray()));
mc = new MongoClient(addr, credentials);
morphia = new Morphia();
morphia.map(ClientData.class);
datastore = morphia.createDatastore(mc, "admin");
datastore.ensureIndexes();
userDAO = new ClientDAO(ClientData.class, datastore);
}
public void disconnect(){
this.mc.close();
}
public ClientData getUserByPlayer(Player player) {
ClientData du = userDAO.findOne("uuid", player.getUniqueId().toString());
long time = System.currentTimeMillis();
if (du == null) {
du = new ClientData();
du.setUuid(player.getUniqueId().toString());
du.setCoins(0);
du.setExp(0);
du.setJoins(0);
du.setLastLoginDate(DateFormat.getTimeInstance().format(time));
du.setLevel(0);
du.setPearls(0);
du.setIp(player.getAddress().getAddress().toString());
du.setUsername(player.getDisplayName());
du.setLastName(player.getName());
du.setHatsOwned(null);
userDAO.save(du);
}
return du;
}
public void saveUser(ClientData user) {
userDAO.save(user);
}
public List<ClientData> getAllUsers() {
return userDAO.find().asList();
}
}
你不能插入多个,因为你有 duplicateKey: 你不能在没有自己设置唯一值的情况下使用原始类型的@id.
你这样定义你的 id:
@Id
public int id;
即使您从未为 id 设置任何值,原始类型也会被初始化为 0
,因此您最终会尝试使用相同的键插入多个文档。
解决方案:
您可以:
- 将您的
id
更改为 String:@Id String id;
- 将您的
id
更改为 ObjectId:@Id ObjectId id;
- 保留
@Id int id
并为其手动设置一个唯一值(例如player.getUniqueId()
)。
前 2 个选项将起作用,因为它们不会被初始化并且将为空。 Mongo 将为您生成一个唯一的 ID。