- 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
824 Views
To concatenate strings from two fields into a third field, you can use the following syntaxdb.yourCollectionName.aggregate( [ { $project: { "yourNewFieldName": { $concat: [ "$yourFieldName1", " yourDellimiterValue ", "$yourFieldName2" ] } } } ] );Let us first create a collection with documents>db.concatenateStringsDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9bb7362d66697741252444") } >db.concatenateStringsDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9bb7402d66697741252445") } >db.concatenateStringsDemo.insertOne({"StudentFirstName":"Carol", "StudentLastName":"Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9bb74c2d66697741252446") } >db.concatenateStringsDemo.insertOne({"StudentFirstName":"David", "StudentLastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9bb7752d66697741252447") } >db.concatenateStringsDemo.insertOne({"StudentFirstName":"James", "StudentLastName":"Williams"}); { "acknowledged" : ... Read More
5K+ Views
To remove _id from MongoDB result, you need to set 0 for _id field. Following is the syntaxdb.yourCollectionName.find({}, {_id:0});To understand it, let us create a collection with documents. Following is the query> db.removeIdDemo.insertOne({"UserName":"John", "UserAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c9bb4042d66697741252440") } > db.removeIdDemo.insertOne({"UserName":"Mike", "UserAge":27}); { "acknowledged" : true, "insertedId" : ObjectId("5c9bb40c2d66697741252441") } > db.removeIdDemo.insertOne({"UserName":"Sam", "UserAge":34}); { "acknowledged" : true, "insertedId" : ObjectId("5c9bb4162d66697741252442") } > db.removeIdDemo.insertOne({"UserName":"Carol", "UserAge":29}); { "acknowledged" : true, "insertedId" : ObjectId("5c9bb4222d66697741252443") }Following is the query to display all documents from a collection with the help of find() method> db.removeIdDemo.find().pretty();This ... Read More
182 Views
You need to use update command along with $pull operator to delete documents within an array. Let us create a collection with documents. Following is the query> db.deleteDocumentsDemo.insertOne( ... { ... "_id":100, ... "StudentsDetails" : [ ... { ... "StudentId" : 1, ... "StudentName" : "John" ... }, ... { ... "StudentId" : 2, ... "StudentName" : "Carol" ... }, ... { ... ... Read More
700 Views
To change the password in MongoDB for existing user, you can use changeUserPassword(). Following is the syntaxdb.changeUserPassword("yourExistingUserName", "yourPassword");Let us first switch the database to admin. Following is the syntax> use adminThis will produce the following outputswitched to db adminNow, display users from the database. Following is the query> db.getUsers();This will produce the following output[ { "_id" : "admin.John", "user" : "John", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] } ]Following is the query to change the password for user “John”> db.changeUserPassword("John", "123456");Now the password has been changed with value “123456”.
128 Views
To operate on all databases from MongoDB shell, you can use listDatabases along with adminCommand().Let’s say we are using a sample database “test”. At first, check the current database with the help of db command.Following is the query to get the current database> db;This will produce the following outputTestFollowing is the query to operate on all the databases from the Mongo shell> var allDatabaseList = db.adminCommand('listDatabases');Now you need to use printjson() in order to print all databases. Following is the query> printjson (allDatabaseList);This will produce the following output{ "databases" : [ { ... Read More
698 Views
Let us first create a collection with documents> db.updateListOfKeyValuesDemo.insertOne( { "StudentDetails":[ { "StudentName":"John", "StudentAge":23, "StudentCountryName":"US" }, { "StudentName":"Carol", "StudentAge":24, "StudentCountryName":"UK" }, { "StudentName":"Bob", "StudentAge":22, "StudentCountryName":"AUS" } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5c9b5b759882024390176545") }Following is the query to display all documents from a collection with the help of find() method> db.updateListOfKeyValuesDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9b5b759882024390176545"), "StudentDetails" : [ { "StudentName" : "John", "StudentAge" : 23, "StudentCountryName" : "US" }, ... Read More
1K+ Views
Yes, it is possible using the $project operator. Let us first create a collection with documents> db.sumTwoFieldsDemo.insertOne({"FirstValue":150, "SecondValue":350}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b4bfe15e86fd1496b38cd") } > db.sumTwoFieldsDemo.insertOne({"FirstValue":450, "SecondValue":1550}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b4c1215e86fd1496b38ce") } > db.sumTwoFieldsDemo.insertOne({"FirstValue":2560, "SecondValue":2440}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b4c2715e86fd1496b38cf") }Following is the query to display all documents from a collection with the help of find() method> db.sumTwoFieldsDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9b4bfe15e86fd1496b38cd"), "FirstValue" : 150, "SecondValue" : 350 } { "_id" : ObjectId("5c9b4c1215e86fd1496b38ce"), "FirstValue" : 450, "SecondValue" ... Read More
965 Views
Following is the query to get distinct values with sorted data in MongoDBdb.yourCollectionName.distinct("yourFieldName").sort();Let us first create a collection with documents>db.getDistinctWithSortedDataDemo.insertOne({"StudentId":10, "StudentName":"John", "StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1e3315e86fd1496b38c3") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":20, "StudentName":"Carol", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1e3e15e86fd1496b38c4") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":10, "StudentName":"John", "StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1e4415e86fd1496b38c5") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":30, "StudentName":"Chris", "StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1e5115e86fd1496b38c6") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":20, "StudentName":"Carol", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1e5715e86fd1496b38c7") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":40, "StudentName":"Bob", "StudentAge":20}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1e6515e86fd1496b38c8") } >db.getDistinctWithSortedDataDemo.insertOne({"StudentId":40, "StudentName":"Bob", "Stude ... Read More
572 Views
There is a $toLower operator in MongoDB to be used as part of aggregate framework. But, we can also use the for loop to iterate over the specific field and update one by one.Let us first create a collection with documents> db.toLowerDemo.insertOne({"StudentId":101, "StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1b4515e86fd1496b38bf") } > db.toLowerDemo.insertOne({"StudentId":102, "StudentName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1b4b15e86fd1496b38c0") } > db.toLowerDemo.insertOne({"StudentId":103, "StudentName":"CHris"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1b5115e86fd1496b38c1") } > db.toLowerDemo.insertOne({"StudentId":104, "StudentName":"ROBERT"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b1b5a15e86fd1496b38c2") }Following is the query to display all documents from ... Read More
84 Views
You can use $push operator for this. Let us first create a collection with documents>db.twoSeparateArraysDemo.insertOne({"StudentName":"Larry", "StudentFirstGameScore":[98], "StudentSecondGameScore":[77]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b152815e86fd1496b38b8") } >db.twoSeparateArraysDemo.insertOne({"StudentName":"Mike", "StudentFirstGameScore":[58], "StudentSecondGameScore":[78]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b152d15e86fd1496b38b9") } >db.twoSeparateArraysDemo.insertOne({"StudentName":"David", "StudentFirstGameScore":[65], "StudentSecondGameScore":[67]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9b153315e86fd1496b38ba") }Following is the query to display all documents from a collection with the help of find() method> db.twoSeparateArraysDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9b152815e86fd1496b38b8"), "StudentName" : "Larry", "StudentFirstGameScore" : [ 98 ], "StudentSecondGameScore" : [ ... Read More