HP
unread,May 30, 2011, 11:49:59 PM5/30/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to mongodb-user
Hello,
I did a basic test on monogoDB version 1.8.1 with Java. The system
alway return me an exception.
I attached my code and all return message as below. I have checked my
code many times, and can not find where is the problem. Please help
to find out the problem for me.
Thanks,
import java.net.UnknownHostException;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
public class QMater_IO {
public static void printAll_QMaster(DBCollection collection){
DBCursor cursor = collection.find();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
}
public static void removeAll_QMaster(DBCollection coll){
coll.remove(new BasicDBObject());
}
public static void insert_QMaster(DBCollection collection){
// int number =0;
System.out.println("Insert routing start..");
BasicDBObject document = new BasicDBObject();
document.put("Number", 100);
document.put("Type", 'A');
document.put("Code", "1100");
document.put("Unit", "ABCD");
collection.insert(document);
System.out.println("Insert routing end..");
}
public static void main(String[] args) {
try {
Mongo mongo = new Mongo();
DB db = mongo.getDB("test");
// get a single collection
DBCollection collection = db.getCollection("QMaster");
removeAll_QMaster(collection);
System.out.println("Testing 1.Print all..");
printAll_QMaster(collection);
insert_QMaster(collection);
System.out.println("insert 1..");
printAll_QMaster(collection);
System.out.println("Done");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
}
Exception in thread "main" java.lang.IllegalArgumentException: can't
serialize class java.lang.Character
at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:213)
at org.bson.BSONEncoder.putObject(BSONEncoder.java:123)
at org.bson.BSONEncoder.putObject(BSONEncoder.java:69)
at com.mongodb.OutMessage.putObject(OutMessage.java:189)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:245)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:209)
at com.mongodb.DBCollection.insert(DBCollection.java:82)
at QMater_IO.insert_QMaster(QMater_IO.java:32)
at QMater_IO.main(QMater_IO.java:51)