- 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
185 Views
Use $elemMatch fir this with $setLet us first create a collection with documents −> dbkeyValueDemoinsertOne( { "_id" : new ObjectId(), "CustomerDetails" : [ { "Name" : "Chris", "Age" :24, }, { "Name" : "Robert", "Age" :29, }, { "Name" : "David", ... Read More
111 Views
Yes, using map(). Let us first create a collection with documents −> dblistOfSpecificValuesDemoinsertOne({"StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5cefcc8fef71edecf6a1f6bb") } > dblistOfSpecificValuesDemoinsertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cefcc94ef71edecf6a1f6bc") } > dblistOfSpecificValuesDemoinsertOne({"StudentName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5cefcc98ef71edecf6a1f6bd") } > dblistOfSpecificValuesDemoinsertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5cefcc9cef71edecf6a1f6be") }Following is the query to display all documents from a collection with the help of find() method −> dblistOfSpecificValuesDemofind();Output{ "_id" : ObjectId("5cefcc8fef71edecf6a1f6bb"), "StudentName" : "John" } { "_id" : ObjectId("5cefcc94ef71edecf6a1f6bc"), "StudentName" : "Chris" } { "_id" : ObjectId("5cefcc98ef71edecf6a1f6bd"), "StudentName" : "Robert" } ... Read More
83 Views
You can use $where operator along with some script.Let us first create a collection with documents −> dbsameValueMultipleTimesDemoinsertOne( { "ListOfPrice":[ {"Price": 110}, {"Price":130}, {"Price": 145} ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cefc4e6ef71edecf6a1f6b9") } > dbsameValueMultipleTimesDemoinsertOne( { "ListOfPrice":[ {"Price": 110}, {"Price":178}, {"Price": 110} ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cefc4e7ef71edecf6a1f6ba") }Following ... Read More
380 Views
For this, use $ifNull operatorLet us first create a collection with documents −> dbquerySelectDemoinsertOne({"Value1":10, "Value2":null}); { "acknowledged" : true, "insertedId" : ObjectId("5cefc0ceef71edecf6a1f6b6") } > dbquerySelectDemoinsertOne({"Value1":null, "Value2":30}); { "acknowledged" : true, "insertedId" : ObjectId("5cefc0d7ef71edecf6a1f6b7") } > dbquerySelectDemoinsertOne({"Value1":60, "Value2":40}); { "acknowledged" : true, "insertedId" : ObjectId("5cefc0e2ef71edecf6a1f6b8") }Display all documents from a collection with the help of find() method −> dbquerySelectDemofind()pretty();Output{ "_id" : ObjectId("5cefc0ceef71edecf6a1f6b6"), "Value1" : 10, "Value2" : null } { "_id" : ObjectId("5cefc0d7ef71edecf6a1f6b7"), "Value1" : null, "Value2" : 30 } { "_id" : ObjectId("5cefc0e2ef71edecf6a1f6b8"), "Value1" : 60, ... Read More
485 Views
To search for month and day only, use the aggregate framework.Let us first create a collection with documents −> db.monthAndDayDemo.insertOne({"LoginDate":new ISODate("2019-04-27")}); { "acknowledged" : true, "insertedId" : ObjectId("5cefbcaeef71edecf6a1f6b2") } > db.monthAndDayDemo.insertOne({"LoginDate":new ISODate("2018-04-30")}); { "acknowledged" : true, "insertedId" : ObjectId("5cefbcb7ef71edecf6a1f6b3") } > db.monthAndDayDemo.insertOne({"LoginDate":new ISODate("2018-04-27")}); { "acknowledged" : true, "insertedId" : ObjectId("5cefbcbcef71edecf6a1f6b4") } > db.monthAndDayDemo.insertOne({"LoginDate":new ISODate("2016-04-27")}); { "acknowledged" : true, "insertedId" : ObjectId("5cefbdaaef71edecf6a1f6b5") }Display all documents from a collection with the help of find() method −> db.monthAndDayDemo.find();Output{ "_id" : ObjectId("5cefbcaeef71edecf6a1f6b2"), "LoginDate" : ISODate("2019-04-27T00:00:00Z") } { "_id" : ObjectId("5cefbcb7ef71edecf6a1f6b3"), "LoginDate" : ISODate("2018-04-30T00:00:00Z") } { "_id" ... Read More
239 Views
To limit number of values in a field, use $slice operator.Let us first create a collection with documents −> db.numberOfValuesDemo.insertOne({"Values":[100,200,300,900,1000,98]}); { "acknowledged" : true, "insertedId" : ObjectId("5cefb736ef71edecf6a1f6ab") }Display all documents from a collection with the help of find() method −> db.numberOfValuesDemo.find().pretty();Output{ "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"), "Values" : [ 100, 200, 300, 900, 1000, 98 ] }Following is the query to limit number of values in a field using MongoDB −> db.numberOfValuesDemo.find({},{ "Values": { "$slice": 2 } } );Output{ "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"), "Values" : [ 100, 200 ] }
71 Views
Let us first create a collection with documents −> db.numberOfValuesDemo.insertOne({"Values":[100,200,300,900,1000,98]}); { "acknowledged" : true, "insertedId" : ObjectId("5cefb736ef71edecf6a1f6ab") }Display all documents from a collection with the help of find() method −> db.numberOfValuesDemo.find().pretty();Output{ "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"), "Values" : [ 100, 200, 300, 900, 1000, 98 ] }Following is the query to get the last two values.Here, we have used -ve sign under $slice −> db.numberOfValuesDemo.find({},{ "Values": { "$slice": -2 } } );Output{ "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"), "Values" : [ 1000, 98 ] }
12K+ Views
To get last inserted document, use sort() along with limit(1).Let us first create a collection with documents −> db.getLastInsertedDocument.insertOne({"Name":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5cefb17eef71edecf6a1f6a8") } > db.getLastInsertedDocument.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cefb181ef71edecf6a1f6a9") } > db.getLastInsertedDocument.insertOne({"Name":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5cefb185ef71edecf6a1f6aa") }Display all documents from a collection with the help of find() method −> db.getLastInsertedDocument.find();Output{ "_id" : ObjectId("5cefb17eef71edecf6a1f6a8"), "Name" : "John" } { "_id" : ObjectId("5cefb181ef71edecf6a1f6a9"), "Name" : "Chris" } { "_id" : ObjectId("5cefb185ef71edecf6a1f6aa"), "Name" : "Robert" }Following is the query to get last inserted document −> db.getLastInsertedDocument.find({}).sort({_id:-1}).limit(1);Output{ "_id" ... Read More
450 Views
Let us first create a collection with documents −> db.findDataDemo.insertOne( { "_id": new ObjectId(), "CustomerName":"John", "CustomerDetails" : { "CountryName" : [ "AUS" ], "isMarried" : [ false ] } } ); { "acknowledged" : true, "insertedId" : ObjectId("5cefa5eeef71edecf6a1f6a5") } > db.findDataDemo.insertOne( { "_id": new ObjectId(), "CustomerName":"Carol", ... Read More
104 Views
Use the distinct() for this, since it finds the distinct values for a specified field across a single collection or view and returns the results in an array.Let us first create a collection with documents −> db.projectionListDemo.insertOne({"_id":"1", "Subject":["MongoDB", "MySQL", "Java"]}); { "acknowledged" : true, "insertedId" : "1" } > db.projectionListDemo.insertOne({"_id":"2", "Subject":["MongoDB", "C", "C++"]}); { "acknowledged" : true, "insertedId" : "2" } > db.projectionListDemo.insertOne({"_id":"3", "Subject":["Java", "Python"]}); { "acknowledged" : true, "insertedId" : "3" }Display all documents from a collection with the help of find() method −> db.projectionListDemo.find().pretty();Output{ "_id" : "1", "Subject" : [ "MongoDB", "MySQL", "Java" ] } { "_id" : ... Read More