0

我正在尝试 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()似乎没有任何效果。谁能指出这个问题?仅供参考,通过控制台插入工作正常。

4

2 回答 2

2
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" : "amar+0@gmail.com" }
{ "_id" : ObjectId("51a83f22fdb3f79d6a713e72"), "email" : "amar+1@gmail.com" }
{ "_id" : ObjectId("51a83f22fdb3f79d6a713e73"), "email" : "amar+2@gmail.com" }
{ "_id" : ObjectId("51a83f22fdb3f79d6a713e74"), "email" : "amar+3@gmail.com" }
{ "_id" : ObjectId("51a83f22fdb3f79d6a713e75"), "email" : "amar+4@gmail.com" }
于 2013-05-31T06:27:04.600 回答
1

我只是更改MongoClient mongoClient = new MongoClient("localhost");Mongo mongo = new Mongo();,一切都按您的预期进行。

您使用的是哪个版本的 mongo 驱动程序?我使用的是 mongo 2.9.1,这里是 maven 依赖项:

<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", "amar+3@gmail.com"));
        int count = email.count();
        System.out.println("count = "+count);
        System.out.println("Total time taken: "+String.valueOf(System.currentTimeMillis()-currentTime));
于 2013-01-26T00:03:49.757 回答