MongoDBをJavaで実行する
MongoDBをJavaのコードで動かしているのですが、うまくいきません。
インサートしたデータをすべて表示させることが目的なのですが、同じデータがなぜか複数回表示されます。
package t.y;
import java.net.UnknownHostException;
import java.util.Arrays;
//import java.util.List;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
public class Sample2 {
public static void main(String[] args) {
try {
/** MongoDB に接続し、Mongo のクライアントオブジェクトを取得 */
MongoClient client = new MongoClient("localhost", 27017);
/** DB オブジェクトを取得 */
DB db = client.getDB("mongo_sample");
/** Collection 名を指定して DBCollection オブジェクトを取得*/
DBCollection member = db.getCollection("ProjectMember");
/** 以下3つのデータを表示させたい */
BasicDBObject insertDocument1 = new BasicDBObject("name", "Serizawa")
.append("age", "26")
.append("country", "JP")
.append("language", Arrays.asList("C++","Java","PHP"))
.append("hobby",Arrays.asList("PCgame","motercycle"))
.append("major", "network engineering");
member.insert(insertDocument1);
BasicDBObject insertDocument2 = new BasicDBObject("name", "Pronk")
.append("age", "25")
.append("country", "US")
.append("language", Arrays.asList("Python","Ruby"))
.append("hobby",Arrays.asList("swimming","traveling"))
.append("major", "AI");
member.insert(insertDocument2);
BasicDBObject insertDocument3 = new BasicDBObject("name", "Lee")
.append("age", "29")
.append("country", "KR")
.append("language", Arrays.asList("HTML","javascript","css"))
.append("hobby",Arrays.asList("music","tennis"))
.append("major", "physics");
member.insert(insertDocument3);
/** 表示するためのコード */
DBCursor cursor = member.find();
try {
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
long count = member.getCount();
System.out.println(count + "件");
}
catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
実行結果(3件なのになぜか63件も・・・・)
{ "_id" : { "$oid" : "5d410ec5d4c648a70dd36e7a"} , "name" : "Serizawa" , "age" : "26" , "country" : "Japan" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d410ec5d4c689c967b31b16"} , "name" : "Serizawa" , "age" : "26" , "country" : "Japan" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d41132cd4c64efea8a1e3b0"} , "name" : "Serizawa" , "age" : "26" , "country" : "Japan" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d41132dd4c67b5912a1657e"} , "name" : "Serizawa" , "age" : "26" , "country" : "Japan" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d411526d4c63458daac096d"} , "name" : "Serizawa" , "age" : "26" , "country" : "Japan" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d411791d4c6acd3a92388ad"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d411791d4c6e8bb9020bf8a"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d411791d4c6e8bb9020bf8b"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d411791d4c6acd3a92388ae"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d412581d4c6ea297c3cfe2a"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d412581d4c6ea297c3cfe2b"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d412581d4c6a6c03e5b14ed"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d412581d4c6a6c03e5b14ee"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d4125bad4c6e59b6cf2d598"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d4125bad4c6e59b6cf2d599"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d4125bbd4c65f7d1cfd7982"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d4125bbd4c65f7d1cfd7983"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d412676d4c675042dba8d7e"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d412676d4c675042dba8d7f"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d412676d4c634cded57e90a"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d412676d4c634cded57e90b"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d412687d4c6d7cee2bf0f1e"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d412687d4c6d7cee2bf0f1f"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d412687d4c67c46990813f0"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d412687d4c67c46990813f1"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d4126a4d4c6e0b319d34f48"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d4126a4d4c6e0b319d34f49"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d4126a4d4c651818369b557"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d4126a4d4c651818369b558"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d4126b6d4c68f8f752b9d69"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d4126b6d4c68f8f752b9d6a"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d4126b7d4c660fee78ac238"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d4126b7d4c660fee78ac239"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d4126c5d4c6982fd8b52dea"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d4126c5d4c6982fd8b52deb"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d4126dbd4c62631f8eb9b2d"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d4126dbd4c62631f8eb9b2e"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d412933d4c6c0deaa7e3428"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d412933d4c6c0deaa7e3429"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d412934d4c6716859c18cdb"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d412934d4c6716859c18cdc"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d412982d4c620b50671bbc7"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d412982d4c620b50671bbc8"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d412983d4c6790606b3c75b"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d412983d4c6790606b3c75c"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d412a58d4c6a456a663f1ec"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d412a58d4c6a456a663f1ed"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d412a58d4c6a456a663f1ee"} , "name" : "Lee" , "age" : "29" , "country" : "KR" , "language" : [ "HTML" , "javascript" , "css"] , "hobby" : [ "music" , "tennis"] , "major" : "physics"}
{ "_id" : { "$oid" : "5d412a58d4c6eaadd3d31db8"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d412a58d4c6eaadd3d31db9"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d412a58d4c6eaadd3d31dba"} , "name" : "Lee" , "age" : "29" , "country" : "KR" , "language" : [ "HTML" , "javascript" , "css"] , "hobby" : [ "music" , "tennis"] , "major" : "physics"}
{ "_id" : { "$oid" : "5d412a9bd4c62d798e0f8307"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d412a9bd4c62d798e0f8308"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d412a9bd4c62d798e0f8309"} , "name" : "Lee" , "age" : "29" , "country" : "KR" , "language" : [ "HTML" , "javascript" , "css"] , "hobby" : [ "music" , "tennis"] , "major" : "physics"}
{ "_id" : { "$oid" : "5d412a9bd4c63dc119502a6f"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d412a9bd4c63dc119502a70"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d412a9bd4c63dc119502a71"} , "name" : "Lee" , "age" : "29" , "country" : "KR" , "language" : [ "HTML" , "javascript" , "css"] , "hobby" : [ "music" , "tennis"] , "major" : "physics"}
{ "_id" : { "$oid" : "5d412bdcd4c6bf19ecd0b781"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d412bdcd4c6bf19ecd0b782"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d412bdcd4c6bf19ecd0b783"} , "name" : "Lee" , "age" : "29" , "country" : "KR" , "language" : [ "HTML" , "javascript" , "css"] , "hobby" : [ "music" , "tennis"] , "major" : "physics"}
{ "_id" : { "$oid" : "5d412bdcd4c65c5beac7fbda"} , "name" : "Serizawa" , "age" : "26" , "country" : "JP" , "language" : [ "C++" , "Java" , "PHP"] , "hobby" : [ "PCgame" , "motercycle"] , "major" : "network engineering"}
{ "_id" : { "$oid" : "5d412bdcd4c65c5beac7fbdb"} , "name" : "Pronk" , "age" : "25" , "country" : "US" , "language" : [ "Python" , "Ruby"] , "hobby" : [ "swimming" , "traveling"] , "major" : "AI"}
{ "_id" : { "$oid" : "5d412bdcd4c65c5beac7fbdc"} , "name" : "Lee" , "age" : "29" , "country" : "KR" , "language" : [ "HTML" , "javascript" , "css"] , "hobby" : [ "music" , "tennis"] , "major" : "physics"}
63件