- 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
281 Views
You can use $set operator for this. Let us first create a collection with documents −> db.pullAllElementDemo.insertOne( ... { ... "StudentId":101, ... "StudentDetails" : [ ... { ... ... "StudentName": "Carol", ... "StudentAge":21, ... "StudentCountryName":"US" ... }, ... { ... "StudentName": "Chris", ... "StudentAge":24, ... ... Read More
517 Views
To update object at specific array index, use update() in MongoDB. Let us first create a collection with documents −> db.updateObjectDemo.insertOne( ... { ... id : 101, ... "StudentDetails": ... [ ... [ ... { ... "StudentName": "John" ... }, ... { "StudentName": "Chris" } ... ], ... [ { "StudentName": "Carol" }, ... Read More
293 Views
To increment a field in MongoDB, you can use $inc operator. Let us first create a collection with documents −> db.incrementDemo.insertOne({"PlayerScore":100}); { "acknowledged" : true, "insertedId" : ObjectId("5cc81cdd8f9e6ff3eb0ce44e") } > db.incrementDemo.insertOne({"PlayerScore":290}); { "acknowledged" : true, "insertedId" : ObjectId("5cc81ce28f9e6ff3eb0ce44f") } > db.incrementDemo.insertOne({"PlayerScore":560}); { "acknowledged" : true, "insertedId" : ObjectId("5cc81ce68f9e6ff3eb0ce450") }Following is the query to display all documents from a collection with the help of find() method −> db.incrementDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cc81cdd8f9e6ff3eb0ce44e"), "PlayerScore" : 100 } { "_id" : ObjectId("5cc81ce28f9e6ff3eb0ce44f"), "PlayerScore" : 290 } { "_id" : ObjectId("5cc81ce68f9e6ff3eb0ce450"), "PlayerScore" : ... Read More
5K+ Views
You can use $eq as well as $ne operator for boolean values. Let us first create a collection with documents −> db.booleanQueryDemo.insertOne({"UserName":"John", "UserAge":23, "isMarried":true}); { "acknowledged" : true, "insertedId" : ObjectId("5cc815c08f9e6ff3eb0ce44a") } > db.booleanQueryDemo.insertOne({"UserName":"Chris", "UserAge":21, "isMarried":false}); { "acknowledged" : true, "insertedId" : ObjectId("5cc815d08f9e6ff3eb0ce44b") } > db.booleanQueryDemo.insertOne({"UserName":"Robert", "UserAge":24, "isMarried":false}); { "acknowledged" : true, "insertedId" : ObjectId("5cc815dc8f9e6ff3eb0ce44c") } > db.booleanQueryDemo.insertOne({"UserName":"David", "UserAge":26, "isMarried":true}); { "acknowledged" : true, "insertedId" : ObjectId("5cc815ed8f9e6ff3eb0ce44d") }Following is the query to display all documents from a collection with the help of find() method −> db.booleanQueryDemo.find().pretty();This will produce the following output −{ ... Read More
239 Views
You can concatenate results with the help of forEach(). Let us first create a collection with documents −> db.concatenateDemo.insertOne({"Name":"John", "Age":21}); { "acknowledged" : true, "insertedId" : ObjectId("5cc80dd88f9e6ff3eb0ce448") } > db.concatenateDemo.insertOne({"Name":"Carol", "Age":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cc80de18f9e6ff3eb0ce449") }Following is the query to display all documents from a collection with the help of find() method −> db.concatenateDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cc80dd88f9e6ff3eb0ce448"), "Name" : "John", "Age" : 21 } { "_id" : ObjectId("5cc80de18f9e6ff3eb0ce449"), "Name" : "Carol", "Age" : 23 }Here is the query to concatenate results ... Read More
108 Views
You can use only $pull operator that removes and updates the existing record in MongoDB. Let us first create a collection with documents −> db.removeDemo.insertOne( ... { ... "UserName" : "Larry", ... "UserDetails" : [ ... { ... "_id" : 101, ... "UserEmailId" : "976Larry@gmail.com", ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cc7f9f88f9e6ff3eb0ce446") } > db.removeDemo.insertOne( ... { ... ... Read More
220 Views
For this, use printjson. Let us first create a collection with documents −> db.cursorDemo.insertOne({"StudentFullName":"John Smith", "StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cc7f0d08f9e6ff3eb0ce442") } > db.cursorDemo.insertOne({"StudentFullName":"John Doe", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5cc7f0df8f9e6ff3eb0ce443") } > db.cursorDemo.insertOne({"StudentFullName":"Carol Taylor", "StudentAge":20}); { "acknowledged" : true, "insertedId" : ObjectId("5cc7f0eb8f9e6ff3eb0ce444") } > db.cursorDemo.insertOne({"StudentFullName":"Chris Brown", "StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5cc7f0f88f9e6ff3eb0ce445") }Following is the query to display all documents from a collection with the help of find() method −> db.cursorDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cc7f0d08f9e6ff3eb0ce442"), "StudentFullName" : "John ... Read More
881 Views
Let us implement the above syntax in order to find all documents in MongoDB with field name “StudentFirstName”. The query is as follows −> db.getCollectionNames().forEach(function(myCollectionName) { ... var frequency = db[myCollectionName].find({"StudentFirstName": {$exists: true}}).count(); ... if (frequency > 0) { ... print(myCollectionName); ... } ... });This will produce the following output −multiDimensionalArrayProjection removeKeyFieldsDemo stringOrIntegerQueryDemoLet us verify the removeKeyFieldsDemo collection has field with name “StudentFirstName” or not. Following is the query −> db.removeKeyFieldsDemo.find({"StudentFirstName":{$exists:true}});This will produce the following output displaying the StudentFirstName field exist −{ "_id" : ObjectId("5cc6c8289cb58ca2b005e672"), "StudentFirstName" : "John", "StudentLastName" : "Doe" } { "_id" ... Read More
876 Views
To pull multiple objects from an array, you can use $pull operator. Let us first create a collection with documents −> db.pullMultipleObjectsDemo.insertOne( ... { ... "ClientId" : "100", ... "ClientName" : "John", ... "ClientPersonalDetails" : [ ... { ... "ClientCountryName" : "US", ... "ClientProjectName" : "Online Book Store", ... ... }, ... { ... "ClientCountryName" : "AUS", ... ... Read More
995 Views
You need to use changeUserPassword() to change a user’s password. Let us first create a user with some roles. Following is the query to create a user in MongoDB −> use admin switched to db admin > db.createUser( ... { ... user: "Chris", ... pwd: "chris", ... roles: [ { role: "readWrite", db: "test" } ] ... } ... ); Successfully added user: { "user" : "Chris", "roles" : [ { "role" : "readWrite", "db" : ... Read More