Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles on Trending Technologies
Technical articles with clear explanations and examples
MongoDB aggregate to get the count of field values of corresponding duplicate names?
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 MoreHow to run MongoDB query to update only a specific field value?
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 MoreFetch records from a subdocument array wherein id begins from 234 in MongoDB
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 MoreMongoDB query (aggregation framework) to match a specific field value
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 MoreQuery MongoDB with “like” implementation on name and email field beginning with a specific letter?
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 MoreHow to select documents with values above the average in MongoDB?
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 MoreMongoDB multiple OR conditions on same key?
For this, simply use $or once. Let us create a collection with documents −> db.demo551.insertOne({"Name":"John"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e36d39e5f92834d7f05e5") } > db.demo551.insertOne({"Name":"Chris Brown"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e36d89e5f92834d7f05e6") } > db.demo551.insertOne({"Name":"John Doe"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e36de9e5f92834d7f05e7") } > db.demo551.insertOne({"Name":"John Smith"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e36e59e5f92834d7f05e8") } > db.demo551.insertOne({"Name":"Carol"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e36ec9e5f92834d7f05e9") }Display all documents from a collection with the help of find() method −> db.demo551.find();This will produce the following output −{ "_id" : ObjectId("5e8e36d39e5f92834d7f05e5"), "Name" : "John" } { "_id" : ObjectId("5e8e36d89e5f92834d7f05e6"), "Name" : "Chris Brown" ...
Read MorePerform multiple updates with bulk operations and update elements in an array in MongoDB
For this, use initializeOrderedBulkOp(). It initializes and returns a new Bulk() operations builder for a collection. The builder constructs an ordered list of write operations that MongoDB executes in bulk.Let us create a collection with documents −>db.demo550.insertOne({"Name":"Chris", "details":[{"Marks":49, Result:"fail"}, {"Marks":58, Result:"fail"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e8e35bd9e5f92834d7f05e4") }Display all documents from a collection with the help of find() method −> db.demo550.find();This will produce the following output −{ "_id" : ObjectId("5e8e35bd9e5f92834d7f05e4"), "Name" : "Chris", "details" : [ { "Marks" : 49, "Result" : "fail" }, { "Marks" : 58, "Result" : "fail" } ] }Following is the query ...
Read MoreMongoDB concurrent update with sub collection?
For update, simply use update(). Use the $push operator to append a specified value and the dot notation to reach the sub collection and update inside update().Let us create a collection with documents −> db.demo547.insertOne( ... { ... Name : "Chris", ... Test : ... { ... "FirstTest" : ... { ... Scores: [56, 29, 76] ... }, ... "SecondTest" : ... { ... Scores: [98, 91, 78] ... } ... ...
Read MoreUsing $redact in MongoDB aggregate?
The $redact restricts the contents of the documents based on information stored in the documents themselves. You can use $cond along with $redact in aggregate. Let us create a collection with documents −> db.demo546.insertOne({"Value1":10, "Value2":20});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e263f9e5f92834d7f05d7") } > db.demo546.insertOne({"Value1":40, "Value2":30, Value3:50});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e26549e5f92834d7f05d8") } > db.demo546.insertOne({"Value1":100, "Value2":200, Value3:null});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e26619e5f92834d7f05d9") } > db.demo546.insertOne({"Value1":400, "Value2":1000, Value3:null});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e26e09e5f92834d7f05da") } > db.demo546.insertOne({"Value1":400, "Value2":200, Value3:null});{ "acknowledged" : true, "insertedId" : ObjectId("5e8e26f59e5f92834d7f05db") } > db.demo546.insertOne({"Value1":400, "Value2":1000, Value3:60});{ "acknowledged" : true, "insertedId" ...
Read More