- 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 1349 Articles for MongoDB
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
124 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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
43 Views
Create an index and then use explain(). Let us create a collection with documents −> db.demo278.ensureIndex({"Subjects":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo278.insertOne({"Subjects":["MySQL", "MongoDB", "Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e49096edd099650a5401a55") } > db.demo278.insertOne({"Subjects":["C", "C++"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e490978dd099650a5401a56") } > db.demo278.insertOne({"Subjects":["Spring", "Hibernate"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e490984dd099650a5401a57") }Display all documents from a collection with the help of find() method −> db.demo278.find();This will produce the following output −{ "_id" : ObjectId("5e49096edd099650a5401a55"), "Subjects" : [ "MySQL", ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
52 Views
Let us first create a collection with documents −> db.demo277.insertOne({"details":[{"FirstName":"John"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e48fb21dd099650a5401a52") } > db.demo277.insertOne({"details":[{"FirstName":"David"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e48fb27dd099650a5401a53") } > db.demo277.insertOne({"details":[{"FirstName":"Chris"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e48fb2bdd099650a5401a54") }Display all documents from a collection with the help of find() method −> db.demo277.find();This will produce the following output −{ "_id" : ObjectId("5e48fb21dd099650a5401a52"), "details" : [ { "FirstName" : "John" } ] } { "_id" : ObjectId("5e48fb27dd099650a5401a53"), "details" : [ { "FirstName" : "David" } ] } { "_id" : ObjectId("5e48fb2bdd099650a5401a54"), "details" : [ { "FirstName" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
515 Views
To split a document by its subdocuments, use $unwind in MongoDB. Let us create a collection with documents −> db.demo276.insertOne({"Name":"Chris", "Subjects":["MySQL", "MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e48f953dd099650a5401a51") }Display all documents from a collection with the help of find() method −> db.demo276.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e48f953dd099650a5401a51"), "Name" : "Chris", "Subjects" : [ "MySQL", "MongoDB" ] }Following is the query to split a document by its subdocuments −> db.demo276.aggregate( [ { $unwind : "$Subjects" } ] )This will produce the following output −{ ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
2K+ Views
To skip records in MongoDB, use skip(). With that, to display only a specific number of records, use limit().Let us create a collection with documents −> db.demo275.insertOne({"Number":10}); { "acknowledged" : true, "insertedId" : ObjectId("5e48eac4dd099650a5401a43") } > db.demo275.insertOne({"Number":12}); { "acknowledged" : true, "insertedId" : ObjectId("5e48eac7dd099650a5401a44") } > db.demo275.insertOne({"Number":6}); { "acknowledged" : true, "insertedId" : ObjectId("5e48eac9dd099650a5401a45") } > db.demo275.insertOne({"Number":1}); { "acknowledged" : true, "insertedId" : ObjectId("5e48eacadd099650a5401a46") } > db.demo275.insertOne({"Number":5}); { "acknowledged" : true, "insertedId" : ObjectId("5e48eacddd099650a5401a47") } > db.demo275.insertOne({"Number":24}); { "acknowledged" : true, "insertedId" : ObjectId("5e48ead0dd099650a5401a48") } > db.demo275.insertOne({"Number":8}); { ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
55 Views
Yes, you can achieve this by indexing like “properties.k” for key and “properties.v” for value. The same is used to be implemented in ensureIndex().Let us first see an example and create a collection with documents −> db.demo274.insertOne({"details":[{StudentFirstName:"Chris", StudentLastName:"Brown"}, ... {StudentFirstName:"David", StudentLastName:"Miller"}, ... {StudentFirstName:"John", StudentLastName:"Smith"}, ... {StudentFirstName:"John", StudentLastName:"Doe"} ...] ...} ...); { "acknowledged" : true, "insertedId" : ObjectId("5e48de35dd099650a5401a42") }Display all documents from a collection with the help of find() method −> db.demo274.find().pretty();OutputThis will produce the following output −{ "_id" : ObjectId("5e48de35dd099650a5401a42"), "details" : [ { "StudentFirstName" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
To insert LONG numbers, use NumberLong(). It is used to handle 64-bit integers. Let us create a collection with documents −> db.demo273.insert({ ... Name:"Robert", ... id: NumberLong("100000000000001"), ... isActive:true ...}) WriteResult({ "nInserted" : 1 }) > db.demo273.insert({ ... Name:"David", ... id: NumberLong("98888888888888888"), ... isActive:false ...}) WriteResult({ "nInserted" : 1 }) > db.demo273.insert({ ... Name:"Mike", ... id: NumberLong("999999999999"), ... isActive:true ...}) WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo273.find();OutputThis will produce the following output −{ "_id" : ObjectId("5e4824df1627c0c63e7dbac3"), "Name" : "Robert", "id" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
85 Views
For this, you can use aggregate() in MongoDB. Let us create a collection with documents −> db.demo149.insertOne({"Status":40}); { "acknowledged" : true, "insertedId" : ObjectId("5e350386fdf09dd6d08539c4") } > db.demo149.insertOne({"Status":40}); { "acknowledged" : true, "insertedId" : ObjectId("5e350388fdf09dd6d08539c5") } > db.demo149.insertOne({"Status":50}); { "acknowledged" : true, "insertedId" : ObjectId("5e35038afdf09dd6d08539c6") }Display all documents from a collection with the help of find() method −> db.demo149.find();This will produce the following output −{ "_id" : ObjectId("5e350386fdf09dd6d08539c4"), "Status" : 40 } { "_id" : ObjectId("5e350388fdf09dd6d08539c5"), "Status" : 40 } { "_id" : ObjectId("5e35038afdf09dd6d08539c6"), "Status" : 50 }Here is the query to MongoDB group by ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
575 Views
To find records that does not match a condition, use $ne. Let us create a collection with documents −> db.demo148.insertOne({"Message":"Hello"}); { "acknowledged" : true, "insertedId" : ObjectId("5e32fb37fdf09dd6d08539c0") } > db.demo148.insertOne({"Message":"Good"}); { "acknowledged" : true, "insertedId" : ObjectId("5e32fb3efdf09dd6d08539c1") } > db.demo148.insertOne({"Message":"Bye"}); { "acknowledged" : true, "insertedId" : ObjectId("5e32fb42fdf09dd6d08539c2") }Display all documents from a collection with the help of find() method −> db.demo148.find();This will produce the following output −{ "_id" : ObjectId("5e32fb37fdf09dd6d08539c0"), "Message" : "Hello" } { "_id" : ObjectId("5e32fb3efdf09dd6d08539c1"), "Message" : "Good" } { "_id" : ObjectId("5e32fb42fdf09dd6d08539c2"), "Message" : "Bye" }Following is the query to ... Read More