【发布时间】:2014-12-20 04:03:27
【问题描述】:
这是我向 mongoDB 插入内容的代码
List<DBObject> write=new ArrayList<DBObject>(); //write is a list of objects
//我正在使用循环从列表中获取每个对象。
DBObject doc = new BasicDBObject("product_name", item.getName()).
append("product_url", item.getUrl()).
append("product_img", item.getImage()).
append("product_price", item.getPrice()).
append("time",item.getTime()).
append("category", item.getCategory());
write.add(doc);
// t = db.getCollection(table); t.insert(写);
当我尝试将价格作为字符串值时没有错误.. 我想将价格更改为双倍值,因为我将价格更改为双倍,然后我将代码更改为
getdouble("product_price", item.getPrice()); //used this instead of .append
然后出错并告诉我将 doc 更改为 double.. 但我无法将 doc 更改为 double 因为其他值是字符串,它会再次显示错误..
请告诉我如何将带有其他字符串值的双精度值插入 mongoDB
我是 MongoDB 新手
【问题讨论】:
-
您不能用 getDouble 替换 append,append 用于设置 mongo 对象值,getDouble(String attrName, double defaultValue) 如果用于在 attrName 的情况下使用默认值获取属性 attrName 中的 double 值不见了。
标签: java mongodb mongodb-query database