- 전체
- Sample DB
- database modeling
- [표준 SQL] Standard SQL
- G-SQL
- 10-Min
- ORACLE
- MS SQLserver
- MySQL
- SQLite
- postgreSQL
- 데이터아키텍처전문가 - 국가공인자격
- 데이터 분석 전문가 [ADP]
- [국가공인] SQL 개발자/전문가
- NoSQL
- hadoop
- hadoop eco system
- big data (빅데이터)
- stat(통계) R 언어
- XML DB & XQuery
- spark
- DataBase Tool
- 데이터분석 & 데이터사이언스
- Engineer Quality Management
- [기계학습] machine learning
- 데이터 수집 및 전처리
- 국가기술자격 빅데이터분석기사
- 암호화폐 (비트코인, cryptocurrency, bitcoin)
NoSQL [mongodb 몽고디비] How to insert multiple document into a MongoDB collection using Java?
2020.10.09 22:04
[mongodb 몽고디비]
How to insert multiple document into a MongoDB collection using Java?
You can insert multiple documents into an existing collection in MongoDB using the insertMany() method.
Syntax
db.coll.insert(docArray)
Where,
-
db is the database.
-
coll is the collection (name) in which you want to insert the document
-
docArray is the array of documents you want to insert.
Example
> use myDatabase() switched to db myDatabase() > db.createCollection(sample) { "ok" : 1 } > db.test.insert([{name:"Ram", age:26, city:"Mumbai"}, {name:"Roja", age:28, city:"Hyderabad"}, {name:"Ramani", age:35, city:"Delhi"}]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 3, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })
Using Java program
In Java, you can create insert a document into a collection using the insertMany() method of the com.mongodb.client.MongoCollection interface. This method accepts a list (object) congaing the documents you want to insert as a parameter.
Therefore to create a collection in MongoDB using Java program −
-
Make sure you have installed MongoDB in your system
-
Add the following dependency to its pom.xml file of your Java project.
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.12.2</version> </dependency>
-
Create a MongoDB client by instantiating the MongoClient class.
-
Connect to a database using the getDatabase() method.
-
Prepare the documents to be inserted.
-
Get the object of the collection into which you want to insert the documents, using the getCollection() method.
-
Create a List object adding all the created documents t it.
-
Invoke the insertMany() method by passing the list object as a parameter.
Example
import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import java.util.ArrayList; import java.util.List; import org.bson.Document; import com.mongodb.MongoClient; public class InsertingMultipleDocuments { public static void main( String args[] ) { //Creating a MongoDB client MongoClient mongo = new MongoClient( "localhost" , 27017 ); //Connecting to the database MongoDatabase database = mongo.getDatabase("myDatabase"); //Creating a collection object MongoCollection<Document> collection = database.getCollection("sampleCollection"); Document document1 = new Document("name", "Ram").append("age", 26).append("city", "Hyderabad"); Document document2 = new Document("name", "Robert").append("age", 27).append("city", "Vishakhapatnam"); Document document3 = new Document("name", "Rhim").append("age", 30).append("city", "Delhi"); //Inserting the created documents List<Document> list = new ArrayList<Document>(); list.add(document1); list.add(document2); list.add(document3); collection.insertMany(list); System.out.println("Documents Inserted"); } }
Output
Documents Inserted
[출처] https://www.tutorialspoint.com/how-to-insert-multiple-document-into-a-mongodb-collection-using-java
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.