MongoDB批量插入不使用java驱动程序

时间:2013-01-25 22:26:47

标签: mongodb

我正在尝试使用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()似乎没有任何工作。有谁可以指出这个问题? FYI在控制台上的插入工作正常。

2 个答案:

答案 0 :(得分: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" }

答案 1 :(得分: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));