出于某种原因,我的Mongo DB只是创建一个属于文档的ID,而不是其他属性。
我有一个Spring @Repository:
@Repository
public class BeerRepository {
public void createBeer(final BeerDTO beerDTO) {
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("beers");
DBCollection beerCollection = db.getCollection(BeerDTO.COLLECTION_NAME);
beerCollection.insert(beerDTO);
// System.err.println(beerCollection.findOne());
}
}
一个简单的POJO,没有别的:
@Document
public class BeerDTO extends BasicDBObject {
private static final long serialVersionUID = 1235041607375829595L;
public static final String COLLECTION_NAME = "Beers";
@Field("id")
private String id;
@Field("name")
private String name;
@Field("abv")
private int abv; //alcohol by volume
public BeerDTO(String id, String name, int abv) {
this.id = id;
this.name = name;
this.abv = abv;
}
}
然而,这是Mongo唯一得到的东西:
非常感谢!
答案 0 :(得分:0)
我在repo中改变了createBeer的方法并且它有效:
public void createBeer(final BeerDTO beerDTO) {
MongoClient mongoClient = new MongoClient();
DB database = mongoClient.getDB("beers");
DBCollection beerCollection = database.getCollection(BeerDTO.COLLECTION_NAME);
DBObject beer = new BasicDBObject("id", beerDTO.getId())
.append("name", beerDTO.getName())
.append("abv", beerDTO.getAbv());
beerCollection.insert(beer);
}