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({})モンゴサービスを再開してみましたが、db.dropDatabase()何も動作しないようです。誰かが問題を指摘できますか?コンソール上のFYIインサートは正常に機能しています。

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 に答える