MongoDB批量插入不适用于java驱动程序 [英] MongoDB batch insert not working with java driver

查看:46
本文介绍了MongoDB批量插入不适用于java驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试用 mongoDB,这是我用来连接和插入记录的代码.

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", "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() 似乎没有任何效果.谁能指出这个问题?仅供参考,控制台上的插入工作正常.

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.

推荐答案

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" }

这篇关于MongoDB批量插入不适用于java驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆