我试图学习如何在Mongo中制作类似于查询的SQL,并发现这个聚合框架,当我运行代码时,虽然“列”中包含数字,但平均值始终为“0”如果我明白了吧mongoDB将所有值保存为一个字符串(在我的sql服务器数据库中我首先将整数转换为mongoDB的数字),所以在使用MongoDB时我真的不必关心类型吗?有人可以看到为什么我的平均功能不工作?订购数量在1.000000 - 7.000000之间。在MongoDB中聚合的问题
DBCollection order = db.getCollection("Orderstatus");
DBObject match = new BasicDBObject("$match", new BasicDBObject("Company", "100"));
DBObject fields = new BasicDBObject("Facility", 1);
fields.put("Ordered quantity", 1);
fields.put("_id", 0);
DBObject project = new BasicDBObject("$project", fields);
DBObject groupFields = new BasicDBObject("_id", "$Facility");
groupFields.put("average", new BasicDBObject("$avg", "$Ordered quantity"));
DBObject group = new BasicDBObject("$group", groupFields);
AggregationOutput output = order.aggregate(match, project,group);
System.out.println(output);
的事情是,我认为,这些数字将作为整数因为我从那里它们被存储为使用下面的代码整数我的SQL Server数据库得到了他们。我现在看到我在使用getString()获取值时,是为什么数字是mongodb中的字符串?我怎样才能得到他们作为整数?我真的希望能够将它们作为数字操作!
StringBuilder orderstatus = new StringBuilder();
orderstatus.append("SELECT * FROM dbo.fact_orderstatus");
PreparedStatement t = connect.prepareStatement(orderstatus.toString());
DBCollection orderstat = db.getCollection("Orderstatus");
ResultSet v = t.executeQuery();
ResultSetMetaData rsm = t.getMetaData();
int column = rsm.getColumnCount();
while (v.next()) {
BasicDBObject orderObj = new BasicDBObject();
for(int x=1; x<column +1; x++){
String namn= rsm.getColumnName(x);
String custNum = (v.getString(x));
if (custNum != null && !custNum.trim().isEmpty()
&& custNum.length() != 0)
orderObj.append(namn, custNum);
}
orderstat.insert(orderObj)
您对类型的理解不正确 - 如果将数字存储为字符串,则无法将它们作为数字进行处理,包括对其进行平均处理。确保将数字存储为数字(不包括引号!) – 2013-04-29 18:57:00