I have been struggling with my Mongo-SQL code for a while and still need your help :)
I have a problem while transferring data from an SQL server database to MongoDB. My problem is that I can't do calculations like AVERAGE()
or SUM()
on my data since I saved them as string in MongoDB. I thought that the numbers would be integers since I got them from my SQL server database where they are stored as integers using the code below. I see now that I use getString()
when getting the values. Is that why the numbers are strings in MongoDB? How can I get them as integers? I really want to be able to manipulate them as numbers! Also, some values in SQL server are datetime, so I will need a lot of 'if' statements and different 'get' methods to get all the types right in MongoDB. Does anyone have a good solution to this problem?
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)