- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 6702 Articles for Database
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
211 Views
To make an array of multiple arrays, use $unwind in MongoDB aggregate. Let us create a collection with documents −> db.demo289.insertOne({"Section":["A", "B", "E"], "Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4c06fcf49383b52759cbc3") } > db.demo289.insertOne({"Section":["C", "D", "B"], "Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4c070af49383b52759cbc4") }Display all documents from a collection with the help of find() method −> db.demo289.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e4c06fcf49383b52759cbc3"), "Section" : [ "A", "B", "E" ], "Name" : "Chris" } { "_id" : ObjectId("5e4c070af49383b52759cbc4"), "Section" : [ "C", "D", "B" ], "Name" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
229 Views
Let us create a collection with documents −>db.demo288.insertOne({"Name":"Chris", details:[{"CountryName":"US", Marks:78}, {"CountryName":"UK", Marks:68}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4c0393f49383b52759cbbe") }Display all documents from a collection with the help of find() method −> db.demo288.find();This will produce the following output −{ "_id" : ObjectId("5e4c0393f49383b52759cbbe"), "Name" : "Chris", "details" : [ { "CountryName" : "US", "Marks" : 78 }, { "CountryName" : "UK", "Marks" : 68 } ] }Following is the query to implement $push in embedded document array −> db.demo288.update( {Name: "Chris"}, {$push:{ "details":{"CountryName" : "AUS", "Marks" : 98} }}, {upsert:true}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
4K+ Views
To get distinct values, use distinct() in MongoDB. It finds the distinct values for a specified field across a single collection or view and returns the results in an array.Let us create a collection with documents −> db.demo287.insertOne({"details":{"AllVowels":["a", "u", "u", "o", "e", "a", "o", "i"]}}); { "acknowledged" : true, "insertedId" : ObjectId("5e4c014cf49383b52759cbbd") }Display all documents from a collection with the help of find() method −> db.demo287.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e4c014cf49383b52759cbbd"), "details" : { "AllVowels" : [ "a", "u", "u", "o", "e", "a", "o", "i" ] } }Following ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
To get the min and max values, use $min and $max. Let us create a collection with documents −> db.demo286.insertOne({"details":[{Value1:70, Value2:50}, {Value1:30, Value2:36}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4ac743f49383b52759cbbc") }Display all documents from a collection with the help of find() method −> db.demo286.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e4ac743f49383b52759cbbc"), "details" : [ { "Value1" : 70, "Value2" : 50 }, { "Value1" : 30, "Value2" : 36 ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
60 Views
The command you can use is db.runCommand(). Let us first switch to admin −> use admin switched to db adminNow, run the command −> db.runCommand({listDatabases : 1})This will produce the following output −{ "databases" : [ { "name" : "admin", "sizeOnDisk" : 2375680, "empty" : false }, { "name" : "app", "sizeOnDisk" : 32768, "empty" : false }, { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
186 Views
For this, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo285.insertOne( ... {... details : [ ... { ... Name : "Chris" ... }, ... { ... Name2: "Bob" ... }, ... { ... Name: "Mike" ... } ... ] ... } ...) { "acknowledged" : true, "insertedId" : ObjectId("5e4abffef49383b52759cbb9") }Display all documents from a collection with the help of find() method −> db.demo285.find();This will produce the ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
371 Views
To export MongoDB has a command mongoexport. Following is the syntax −mongoexport -d yourDatabaseName -c yourCollectionName -f yourFieldName --type=csv -o yourFileLocation/FileName;Let us create a collection with documents −> db.demo284.insertOne({"FirstName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4abc9e9127fafea82a2cfc") } > db.demo284.insertOne({"FirstName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4abca39127fafea82a2cfd") } > db.demo284.insertOne({"FirstName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4abca79127fafea82a2cfe") }Display all documents from a collection with the help of find() method −> db.demo284.find();This will produce the following output −{ "_id" : ObjectId("5e4abc9e9127fafea82a2cfc"), "FirstName" : "Chris" } { "_id" : ObjectId("5e4abca39127fafea82a2cfd"), "FirstName" : "Robert" } { "_id" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
275 Views
To update array with another field, use $push. Let us create a collection with documents −> db.demo283.insertOne({"Name":"Chris", "Status":["Active", "Inactive"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4ab97ddd099650a5401a75") }Display all documents from a collection with the help of find() method −> db.demo283.find();This will produce the following output −{ "_id" : ObjectId("5e4ab97ddd099650a5401a75"), "Name" : "Chris", "Status" : [ "Active", "Inactive" ] }Following is the query to update array with another field −> db.demo283.update({}, {$push: {Status:{$each:['Regular', 'NotRegular'], '$slice': -4}}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Display all documents from a collection with the help of find() method −> ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
125 Views
To retrieve document from collection by _id, use find() with $in. Let us create a collection with documents −> db.demo281.insertOne({"Name":"Chris", "Age":21}); { "acknowledged" : true, "insertedId" : ObjectId("5e4aac28dd099650a5401a66") } > db.demo281.insertOne({"Name":"Bob", "Age":23}); { "acknowledged" : true, "insertedId" : ObjectId("5e4aac46dd099650a5401a67") } > db.demo281.insertOne({"Name":"David", "Age":28}); { "acknowledged" : true, "insertedId" : ObjectId("5e4aac4fdd099650a5401a68") }Display all documents from a collection with the help of find() method −> db.demo281.find();This will produce the following output −{ "_id" : ObjectId("5e4aac28dd099650a5401a66"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5e4aac46dd099650a5401a67"), "Name" : "Bob", "Age" : 23 } { "_id" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
228 Views
To delete element from an array, use $pull. Let us create a collection with documents −> db.demo279.insertOne({id:[101, 103, 105, 110]}); { "acknowledged" : true, "insertedId" : ObjectId("5e490af7dd099650a5401a58") } > db.demo279.insertOne({id:[107, 111, 110]}); { "acknowledged" : true, "insertedId" : ObjectId("5e490b06dd099650a5401a59") }Display all documents from a collection with the help of find() method −> db.demo279.find();This will produce the following output −{ "_id" : ObjectId("5e490af7dd099650a5401a58"), "id" : [ 101, 103, 105, 110 ] } { "_id" : ObjectId("5e490b06dd099650a5401a59"), "id" : [ 107, 111, 110 ] }Following is the query to delete element from an array &minus';> db.demo279.update({}, {$pull:{id:110}}, {multi:true}); ... Read More