- 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
898 Views
To filter and fetch, use projection along with MongoDB $filter and $match. Let us create a collection with documents −> db.demo507.insertOne( ... { ... ... "Information": ... [ ... {"Name":"John", "SubjectName":"MySQL"}, ... {"Name":"Bob", "SubjectName":"MongoDB"}, ... {"Name":"Chris", "SubjectName":"MySQL"}, ... {"Name":"David", "SubjectName":"C++"} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e8836d3987b6e0e9d18f577") }Display all documents from a collection with the help of find() method −> db.demo507.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e8836d3987b6e0e9d18f577"), "Information" : [ ... Read More
643 Views
To increment the value of an array object, use $inc. Let us create a collection with documents −>db.demo506.insertOne({"details":[{id:1, Quantity:4}, {id:2, Quantity:3}, {id:3, Quantity:2}, {id:4, Qua ntity:7}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e882ed6987b6e0e9d18f576") }Display all documents from a collection with the help of find() method −> db.demo506.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e882ed6987b6e0e9d18f576"), "details" : [ { "id" : 1, "Quantity" : 4 }, { "id" : 2, ... Read More
2K+ Views
To sort nested array in MongoDB, use $sort. Let us create a collection with documents −> db.demo505.insertOne( ... { ... "details": [ ... { ... Name:"Chris", ... "Score":58 ... }, { ... ... Name:"Bob", ... "Score":45 ... }, { ... ... Name:"John", ... "Score":68 ... }, { ... ... Name:"David", ... "Score":46 ... } ... Read More
252 Views
To find the user by name in MongoDB, use find(). Let us create a collection with documents −> db.demo504.insertOne({"Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8823ee987b6e0e9d18f570") } > db.demo504.insertOne({"Name":"John"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8823f2987b6e0e9d18f571") } > db.demo504.insertOne({"Name":"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8823f5987b6e0e9d18f572") }Display all documents from a collection with the help of find() method −> db.demo504.find();This will produce the following output −{ "_id" : ObjectId("5e8823ee987b6e0e9d18f570"), "Name" : "Chris" } { "_id" : ObjectId("5e8823f2987b6e0e9d18f571"), "Name" : "John" } { "_id" : ObjectId("5e8823f5987b6e0e9d18f572"), "Name" : "David" }Following is the query to find a user by ... Read More
170 Views
To slice array, use a $slice operator in MongoDB. Let us create a collection with documents −> db.demo503.insertOne({_id:1, Name:"John", Subject:["MySQL", "Java", "C"]}); { "acknowledged" : true, "insertedId" : 1 } > db.demo503.insertOne({_id:2, Name:"David", Subject:["MongoDB", "C++", "Python"]}); { "acknowledged" : true, "insertedId" : 2 }Display all documents from a collection with the help of find() method −> db.demo503.find().pretty();This will produce the following output −{ "_id" : 1, "Name" : "John", "Subject" : [ "MySQL", "Java", "C" ] } { "_id" : 2, "Name" : "David", "Subject" : [ "MongoDB", "C++", ... Read More
582 Views
For combination, use $concat and check the equality using $eq. Let us create a collection with documents −> db.demo502.insertOne({"FirstName":"John", "LastName":"Smith"});{ "acknowledged" : true, "insertedId" : ObjectId("5e875534987b6e0e9d18f56d") } > db.demo502.insertOne({"FirstName":"David", "LastName":"Miller"});{ "acknowledged" : true, "insertedId" : ObjectId("5e87553e987b6e0e9d18f56e") } > db.demo502.insertOne({"FirstName":"John", "LastName":"Doe"});{ "acknowledged" : true, "insertedId" : ObjectId("5e875543987b6e0e9d18f56f") }Display all documents from a collection with the help of find() method −> db.demo502.find();This will produce the following output −{ "_id" : ObjectId("5e875534987b6e0e9d18f56d"), "FirstName" : "John", "LastName" : "Smith" } { "_id" : ObjectId("5e87553e987b6e0e9d18f56e"), "FirstName" : "David", "LastName" : "Miller" } { "_id" : ObjectId("5e875543987b6e0e9d18f56f"), "FirstName" : "John", ... Read More
324 Views
To group duplicate documents, use MongoDB aggregate(). Let us create a collection with documents −> db.demo501.insertOne({"Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8752f0987b6e0e9d18f566") } > db.demo501.insertOne({"Name":"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8752f4987b6e0e9d18f567") } > db.demo501.insertOne({"Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8752f8987b6e0e9d18f568") } > db.demo501.insertOne({"Name":"John"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8752fb987b6e0e9d18f569") } > db.demo501.insertOne({"Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8752fd987b6e0e9d18f56a") } > db.demo501.insertOne({"Name":"John"});{ "acknowledged" : true, "insertedId" : ObjectId("5e875301987b6e0e9d18f56b") } > db.demo501.insertOne({"Name":"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e875307987b6e0e9d18f56c") }Display all documents from a collection with the help of ... Read More
125 Views
For this, use skip() in MongoDB. Under skip(), set “count() – 10” to get 10 most recent documents. Let us create a collection with documents −> db.demo500.insertOne({value:10});{ "acknowledged" : true, "insertedId" : ObjectId("5e8749c5987b6e0e9d18f55a") } > db.demo500.insertOne({value:1200});{ "acknowledged" : true, "insertedId" : ObjectId("5e8749c8987b6e0e9d18f55b") } > db.demo500.insertOne({value:19});{ "acknowledged" : true, "insertedId" : ObjectId("5e8749cb987b6e0e9d18f55c") } > db.demo500.insertOne({value:28});{ "acknowledged" : true, "insertedId" : ObjectId("5e8749cf987b6e0e9d18f55d") } > db.demo500.insertOne({value:50});{ "acknowledged" : true, "insertedId" : ObjectId("5e8749d1987b6e0e9d18f55e") } > db.demo500.insertOne({value:70});{ "acknowledged" : true, "insertedId" : ObjectId("5e8749d4987b6e0e9d18f55f") } > db.demo500.insertOne({value:100});{ "acknowledged" : true, "insertedId" : ... Read More
392 Views
To query an array of subdocuments, use $unwind in MongoDB. Let us create a collection with documents −> db.demo499.insertOne({ ... "details": ... [ ... { ... Name :"MIT", ... Rank: 1, ... "CountryName":"US" ... }, ... ... { ... Name :"Stanford University", ... Rank: 2 ... }, ... ... { ... Name :"University of California, Berkeley", ... ... Read More
1K+ Views
To retrieve group by result with arrays, use aggregate(). We will also use $addToSet operator. It adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array.Let us create a collection with documents −> db.demo498.insertOne({id:1, Name:["Chris"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e86192b987b6e0e9d18f553") } > db.demo498.insertOne({id:2, Name:["David"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e86192d987b6e0e9d18f554") } > db.demo498.insertOne({id:3, Name:["Chris"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e861931987b6e0e9d18f555") } > db.demo498.insertOne({id:4, Name:["Bob"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e861942987b6e0e9d18f556") } > db.demo498.insertOne({id:5, Name:["David"]});{ "acknowledged" : true, "insertedId" ... Read More