我正在尝试 mongoDB,这是我用来连接和插入记录的代码。
import com.mongodb.*;
import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.List;
public class MongoConnect {
public static void main(String[] args) throws UnknownHostException, InterruptedException {
MongoClient mongoClient = new MongoClient("localhost");
DB db = mongoClient.getDB("mydb");
DBCollection collection = db.getCollection("emails");
long currentTime = System.currentTimeMillis();
long totalRecords = 120L;
long batchInsert = 0;
long insertedRecords = 0L;
List<DBObject> basicDBObjects = new LinkedList<DBObject>();
while (insertedRecords < totalRecords) {
System.out.println("adding: "+insertedRecords);
basicDBObjects.add(new BasicDBObject("email", "amar+" + insertedRecords + "@gmail.com"));
insertedRecords++;
batchInsert++;
if (batchInsert == 5) {
System.out.println("inserting: "+(insertedRecords-5));
collection.insert(basicDBObjects);
System.out.println("Inserted: *******"+insertedRecords);
//Thread.sleep(200);
batchInsert = 0;
basicDBObjects = new LinkedList<DBObject>();
}
}
long endTime = System.currentTimeMillis();
System.out.println("Total time taken :"+((endTime-currentTime)/1000));
//long currentTime = System.currentTimeMillis();
DBCursor email = collection.find(new BasicDBObject("email", "amar+3@gmail.com"));
int count = email.count();
System.out.println("count = "+count);
System.out.println("Total time taken: "+String.valueOf(System.currentTimeMillis()-currentTime));
}
}
我可以看到创建了带有“电子邮件”的集合,它显示为的一部分show collections
但是当我db.mydb.emails.find({})
没有结果时。我尝试重新启动 mongo 服务,甚至尝试db.dropDatabase()
似乎没有任何效果。谁能指出这个问题?仅供参考,通过控制台插入工作正常。