找到你要的答案

Q:BasicDBObject in MongoDB refusing to take value integer

Q:在MongoDB中拒绝接受值的整数basicdbobject

I cannot add the value 30 to key age i am getting error that says: The method put(String, Object) in the type BasicBSONObject is not applicable for the arguments (String, int). also for newDate method

  BasicDBObject document = new BasicDBObject();
  document.put("name", "baja");
  document.put("age", 30);
  document.put("createdDate", newDate());
  table.insert(document);

我无法将值30的关键年龄我得到错误,说:该方法把(字符串对象)的类型basicbsonobject不适用于参数(String,int)。也为newdate方法

  BasicDBObject document = new BasicDBObject();
  document.put("name", "baja");
  document.put("age", 30);
  document.put("createdDate", newDate());
  table.insert(document);
answer1: 回答1:
private static MongoDatabase db;
private static final String collectionName = "tweets";


final MongoClientURI uri = new MongoClientURI(DB_URL); 
//example mongodb://localhost:27017/databaseName 

final MongoClient client = new MongoClient(uri);
db = client.getDatabase(uri.getDatabase());

 Document document = new Document();
 document.put("name", "baja");
 document.put("age", 30);
 document.put("createdDate",new Date());
 db.getCollection(collectionName).insertOne(document);
private static MongoDatabase db;
private static final String collectionName = "tweets";


final MongoClientURI uri = new MongoClientURI(DB_URL); 
//example mongodb://localhost:27017/databaseName 

final MongoClient client = new MongoClient(uri);
db = client.getDatabase(uri.getDatabase());

 Document document = new Document();
 document.put("name", "baja");
 document.put("age", 30);
 document.put("createdDate",new Date());
 db.getCollection(collectionName).insertOne(document);
java  mongodb