
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How do I insert a record from one Mongo database into another?
You can switch from one database to another using the use command. Here, we are using the collection in the “test” database. Let us insert that collection in another database with the name “sample”.
To understand further, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.insertOneRecordDemo.insertOne({"UserName":"Larry","UserAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c9534de16f542d757e2b452") } > db.insertOneRecordDemo.insertOne({"UserName":"Chris","UserAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c9534e816f542d757e2b453") } > db.insertOneRecordDemo.insertOne({"UserName":"David","UserAge":25}); { "acknowledged" : true, "insertedId" : ObjectId("5c9534f116f542d757e2b454") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.insertOneRecordDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c9534de16f542d757e2b452"), "UserName" : "Larry", "UserAge" : 23 } { "_id" : ObjectId("5c9534e816f542d757e2b453"), "UserName" : "Chris", "UserAge" : 26 } { "_id" : ObjectId("5c9534f116f542d757e2b454"), "UserName" : "David", "UserAge" : 25 }
Here is the query to insert a record from one MongoDB database into another −
> var AllDocumentsFromSourceCollection = db.insertOneRecordDemo.find(); > use sample; switched to db sample > AllDocumentsFromSourceCollection.forEach(function(allRecords){ db.getAllRecordsFromSourceCollectionDemo.insert(allRecords) });
Check the record has been inserted or not. The query is as follows −
> db.getAllRecordsFromSourceCollectionDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c9534de16f542d757e2b452"), "UserName" : "Larry", "UserAge" : 23 } { "_id" : ObjectId("5c9534e816f542d757e2b453"), "UserName" : "Chris", "UserAge" : 26 } { "_id" : ObjectId("5c9534f116f542d757e2b454"), "UserName" : "David", "UserAge" : 25 }
Now you can check the collection name is present or not in the sample database. The query is as follows −
> show collections;
The following is the output −
arraySizeErrorDemo basicInformationDemo copyThisCollectionToSampleDatabaseDemo deleteAllRecordsDemo deleteDocuments deleteDocumentsDemo deleteSomeInformation documentWithAParticularFieldValueDemo employee findListOfIdsDemo findSubstring getAllRecordsFromSourceCollectionDemo getElementWithMaxIdDemo internalArraySizeDemo largestDocumentDemo makingStudentInformationClone oppositeAddToSetDemo prettyDemo returnOnlyUniqueValuesDemo selectWhereInDemo sourceCollection studentInformation sumOfValueDemo truncateDemo updateInformation userInformation
- Related Articles
- How do I insert all elements from one list into another in Java?
- How do I INSERT INTO from one MySQL table into another table and set the value of one column?
- How to insert a record into a table in a database using JDBC API?
- How to insert values from one table into another in PostgreSQL?
- How do I get the id after INSERT into MySQL database in Python?
- Select some data from a database table and insert into another table in the same database with MySQL
- How do we insert/store a file into MySQL database using JDBC?
- MySQL statement to copy data from one table and insert into another table
- How do I insert a JPEG image into a Python Tkinter window?
- How to copy a table from one MySQL database to another?
- How to insert DECIMAL into MySQL database?
- How to copy a collection from one database to another in MongoDB?
- How to insert data into a MySQL database with Java?
- How to select a random record from a MySQL database?
- How do I return a document with filtered sub-documents using Mongo?
