- 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
66 Views
To randomize unique data, use Math.random() in MongoDB. Let us create a collection with documents −> db.demo561.insertOne({EmailId:null});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f490454b4472ed3e8e86c") } > db.demo561.insertOne({EmailId:null});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f490654b4472ed3e8e86d") } > db.demo561.insertOne({EmailId:null});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f490a54b4472ed3e8e86e") }Display all documents from a collection with the help of find() method −> db.demo561.find();This will produce the following output −{ "_id" : ObjectId("5e8f490454b4472ed3e8e86c"), "EmailId" : null } { "_id" : ObjectId("5e8f490654b4472ed3e8e86d"), "EmailId" : null } { "_id" : ObjectId("5e8f490a54b4472ed3e8e86e"), "EmailId" : null }Following is the query for randomizing unique data with MongoDB −> db.demo561.find().forEach(function(doc){ ... ... Read More
Fetch data between two dates and with a specific value in MongoDB. Group and get the sum with count?
585 Views
To match, use $match in MongoDB and to get data between two dates, use $gte and $lte. Let us create a collection with documents −> db.demo560.insertOne({"value1":40, "value2":40, shippingDate:new ISODate("2020-02-26")});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f3d5254b4472ed3e8e867") } > db.demo560.insertOne({"value1":20, "value2":60, shippingDate:new ISODate("2020-02-26")});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f3d5254b4472ed3e8e868") } > db.demo560.insertOne({"value1":40, "value2":70, shippingDate:new ISODate("2020-03-31")});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f3d5254b4472ed3e8e869") } > db.demo560.insertOne({"value1":40, "value2":130, shippingDate:new ISODate("2020-03-31")});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f3d5254b4472ed3e8e86a") }Display all documents from a collection with the help of find() method −> db.demo560.find();This will produce the following output −{ "_id" : ObjectId("5e8f3d5254b4472ed3e8e867"), "value1" : ... Read More
117 Views
To GROUP BY array of the document, use $group. Let us create a collection with documents −>db.demo559.insertOne({details:[{Name:"Chris", Age:21}, {Name:"Bob", Age:22}, {Name:"Carol", Age:21}, {Name:"Sam", Age:21}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e8f38d954b4472ed3e8e866") }Display all documents from a collection with the help of find() method −> db.demo559.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e8f38d954b4472ed3e8e866"), "details" : [ { "Name" : "Chris", "Age" : 21 }, { "Name" : "Bob", "Age" : ... Read More
1K+ Views
Let us see an example and create a collection with documents −> db.demo558.insertOne( ... { ... _id : 100, ... CountryCode:101, ... details: [ ... { ... Name:"Chris", ... Subject:"MySQL" ... }, ... { ... Name:"Chris", ... Subject:"MongoDB" ... }, ... { ... Name:"Chris", ... Subject:"Java" ... }, ... { ... ... Read More
154 Views
Let us see an example and create a collection with documents −> db.demo557.insertOne({Name:"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f28e954b4472ed3e8e864") } > db.demo557.insertOne({Name:"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f28ee54b4472ed3e8e865") }Display all documents from a collection with the help of find() method −> db.demo557.find();This will produce the following output −{ "_id" : ObjectId("5e8f28e954b4472ed3e8e864"), "Name" : "Chris" } { "_id" : ObjectId("5e8f28ee54b4472ed3e8e865"), "Name" : "David" }Following is the query to update only a specific field value −> db.getCollection('demo557').update({Name:"Chris"}, {$set:{Name:"Robert"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Display all documents from a collection with the help of find() method ... Read More
58 Views
To fetch records from a subdocument array, use $unwind along with $push. For ids beginning from 234, use regex in MongoDB.Let us create a collection with documents −> db.demo556.insertOne( ... { ... _id:101, ... details:[ ... { ... id:"234336", ... Name:"Chris" ... }, ... { ... id:"123456", ... Name:"Bob" ... }, ... { ... id:"234987", ... Name:"Carol" ... ... Read More
243 Views
To match a specific field value, use a $match in MongoDB aggregation. Let us create a collection with documents −> db.demo555.insertOne({"CountryName":"US"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f21bf54b4472ed3e8e85f") } > db.demo555.insertOne({"CountryName":"UK"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f21c254b4472ed3e8e860") } > db.demo555.insertOne({"CountryName":"US"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f21c354b4472ed3e8e861") } > db.demo555.insertOne({"CountryName":"AUS"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f21c554b4472ed3e8e862") } > db.demo555.insertOne({"CountryName":"US"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f21c754b4472ed3e8e863") }Display all documents from a collection with the help of find() method −> db.demo555.find();This will produce the following output: −{ "_id" : ObjectId("5e8f21bf54b4472ed3e8e85f"), "CountryName" : "US" } { "_id" : ObjectId("5e8f21c254b4472ed3e8e860"), "CountryName" ... Read More
760 Views
For “like” implementation in MongoDB, use / / and set that specific letter in between. For example −/J/Let us create a collection with documents −> db.demo554.insertOne({"UserName":"John", "UserMailId":"John@gmail.com"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f1cfed1d72c4545cb8679") } > db.demo554.insertOne({"UserName":"Chris", "UserMailId":"Chris@gmail.com"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f1d0cd1d72c4545cb867a") } > db.demo554.insertOne({"UserName":"Jace", "UserMailId":"Jace@gmail.com"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f1d1cd1d72c4545cb867b") }Display all documents from a collection with the help of find() method −> db.demo554.find();This will produce the following output −{ "_id" : ObjectId("5e8f1cfed1d72c4545cb8679"), "UserName" : "John", "UserMailId" : "John@gmail.com" } { "_id" : ObjectId("5e8f1d0cd1d72c4545cb867a"), "UserName" : "Chris", "UserMailId" : "Chris@gmail.com" } { "_id" : ... Read More
3K+ Views
For this, use findOneAndUpdate() in MongoDB. The findOneAndUpdate() method updates a single document based on the filter and sort criteria.Let us create a collection with documents −> db.demo553.insertOne( ... { ... id:101, ... "Name":"John", ... midExamDetails: ... [ ... {"SubjectName":"MySQL", "Marks":70}, ... {"SubjectName":"MongoDB", "Marks":35} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e8e3da19e5f92834d7f05ed") }Display all documents from a collection with the help of find() method −> db.demo553.find();This will produce the following output −{ "_id" : ObjectId("5e8e3da19e5f92834d7f05ed"), "id" : 101, "Name" : "John", "midExamDetails" ... Read More
382 Views
Use aggregate() in MongoDB to select documents with values above the average. To find the average, use $avg in MongoDB.Let us create a collection with documents −> db.demo552.insertOne({values:10});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e3b1c9e5f92834d7f05ea") } > db.demo552.insertOne({values:50});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e3b1f9e5f92834d7f05eb") } > db.demo552.insertOne({values:40});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e3b289e5f92834d7f05ec") }Display all documents from a collection with the help of find() method −> db.demo552.find();This will produce the following output −{ "_id" : ObjectId("5e8e3b1c9e5f92834d7f05ea"), "values" : 10 } { "_id" : ObjectId("5e8e3b1f9e5f92834d7f05eb"), "values" : 50 } { "_id" : ObjectId("5e8e3b289e5f92834d7f05ec"), "values" : 40 }Following is the ... Read More