Reputation: 2064
I am trying out mongoDB, here is the code I used to connect and insert records.
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", "[email protected]"));
int count = email.count();
System.out.println("count = "+count);
System.out.println("Total time taken: "+String.valueOf(System.currentTimeMillis()-currentTime));
}
}
I can see the collection with "emails" is created it is shown as part of show collections
But when I do db.mydb.emails.find({})
no result is coming up. I tried re-starting mongo service and even tried db.dropDatabase()
nothing seems to work. Can anyone point out the issue? FYI inserts over console are working fine.
Upvotes: 0
Views: 2706
Reputation: 734
This code is working with mongoDB Version 2.2.3
So Pleae install this version and check your db
First Perform following command
show dbs
use mydb
show collections
when you perform these three command then you will see list of collection for mydb
then perform
db.emails.find() which will give you will get Record
Here below i have paste command that i have fire it and check it dear
C:\dhananjay\mongoDB\mongodb\bin>mongo.exe
MongoDB shell version: 2.2.3
connecting to: test
> show dbs
blog 0.203125GB
course 0.203125GB
local (empty)
m101 0.203125GB
mydb 0.203125GB
school 0.203125GB
students 0.203125GB
test 0.203125GB
> use mydb
switched to db mydb
> show collections
emails
system.indexes
> db.emails.find()
{ "_id" : ObjectId("51a83f22fdb3f79d6a713e71"), "email" : "[email protected]" }
{ "_id" : ObjectId("51a83f22fdb3f79d6a713e72"), "email" : "[email protected]" }
{ "_id" : ObjectId("51a83f22fdb3f79d6a713e73"), "email" : "[email protected]" }
{ "_id" : ObjectId("51a83f22fdb3f79d6a713e74"), "email" : "[email protected]" }
{ "_id" : ObjectId("51a83f22fdb3f79d6a713e75"), "email" : "[email protected]" }
Upvotes: 2
Reputation: 7920
I only changed MongoClient mongoClient = new MongoClient("localhost");
to Mongo mongo = new Mongo();
and everything worked as you expected.
Which version of mongo driver you are using? I am using mongo 2.9.1 here are the maven dependencies:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>2.9.1</version>
</dependency>
Mongo mongo = new Mongo();
DB db = mongo.getDB("example");
DBCollection collection = db.getCollection("sampleCollection");
List<DBObject> basicDBObjects = Lists.newArrayList();
long currentTime = System.currentTimeMillis();
long totalRecords = 120L;
long batchInsert = 0;
long insertedRecords = 0L;
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 = Lists.newArrayList();
}
}
long endTime = System.currentTimeMillis();
System.out.println("Total time taken :"+((endTime-currentTime)/1000));
//long currentTime = System.currentTimeMillis();
DBCursor email = collection.find(new BasicDBObject("email", "[email protected]"));
int count = email.count();
System.out.println("count = "+count);
System.out.println("Total time taken: "+String.valueOf(System.currentTimeMillis()-currentTime));
Upvotes: 1