- 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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
481 Views
JavaScript function can be saved for reuse using a system collection called system.js. To store a function, use the db.collection.save(),Let us first create a function. Following is the query −> db.system.js.save({ ... _id: "displayMessage", ... value: function (data) { ... return 'The Name is: ' + data; ... } ... })This will produce the following output −WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "displayMessage" })Following is the query to execute stored function −> db.eval("displayMessage('John')") WARNING: db.eval is deprecatedThis will produce the following output −The Name is: John
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
232 Views
There is no difference between show dbs and show databases. Both commands internally call listDatabases command.The show dbs command is as follows −> show dbsThis will produce the following output −admin 0.002GB app 0.000GB business 0.000GB config 0.000GB local 0.000GB main 0.000GB ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
506 Views
To find when the keys are unknown, use $addField and $objectToArray. Let us first create a collection with documents −> db.demo375.insertOne( ... { ... "details":{ ... "Name":"John", ... "Age":23 ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5a0ae42ae06a1609a00b06") } > db.demo375.insertOne( ... { ... "details":{ ... "Name":"David", ... "Age":21 ... } ... } ... ); { "acknowledged" : true, "insertedId" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
385 Views
To group by another field, use $group along with $project. Let us first create a collection with documents −> db.demo374.insertOne( ... { ... ... "Name" : "Chris", ... "HobbyDetails" : [ ... "Reading Book", ... "Playing Football" ... ], ... "CountryName" : "US" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5a04402ae06a1609a00b04") } > db.demo374.insertOne( ... { ... ... "Name" : "Chris", ... "HobbyDetails" : [ ... ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
324 Views
Let us first create a collection with documents −> db.demo373.createIndex({"Name":1, "CountryName":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo373.insertOne({"Name":"Chris", "Age":22, "CountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e59ffde2ae06a1609a00aff") } > db.demo373.insertOne({"Name":"David", "Age":21, "CountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5e59ffe82ae06a1609a00b00") } > db.demo373.insertOne({"Name":"Bob", "Age":23, "CountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5e59fff42ae06a1609a00b01") } > db.demo373.insertOne({"Name":"John", "Age":21, "CountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e59ffff2ae06a1609a00b02") } > db.demo373.insertOne({"Name":"Carol", "Age":23, "CountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5e5a00082ae06a1609a00b03") }Display all ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
The $ifNull evaluates an expression and returns the value of the expression if the expression evaluates to a non-null value.Let us first create a collection with documents −> db.demo372.insertOne({"FirstName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e591aea2ae06a1609a00af6") } > db.demo372.insertOne({"FirstName":null}); { "acknowledged" : true, "insertedId" : ObjectId("5e591aef2ae06a1609a00af7") } > db.demo372.insertOne({"FirstName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e591af42ae06a1609a00af8") } > db.demo372.insertOne({"FirstName":null}); { "acknowledged" : true, "insertedId" : ObjectId("5e591afb2ae06a1609a00af9") }Display all documents from a collection with the help of find() method −> db.demo372.find();This will produce the following output −{ "_id" : ObjectId("5e591aea2ae06a1609a00af6"), "FirstName" : "Chris" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
To change the primary key, you need to first delete it. Use forEach() along with delete to remove and then get a new primary key. Let us create a collection with documents −> db.demo41.insertOne({"StudentName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5e25ce4acfb11e5c34d898e3") }Display all documents from a collection with the help of find() method −> db.demo41.find();This will produce the following output −{ "_id" : ObjectId("5e25ce4acfb11e5c34d898e3"), "StudentName" : "Carol" }Here is the query to change the primary key on a MongoDB collection −> var next = db.demo41.find() > > next.forEach(function(s) { ... var prevId=s._id; ... delete s._id; ... ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
1K+ Views
For this, use save() in MongoDB. Following is the syntax −var anyVaribaleName=yourValue db.anyCollectionName.save(yourVariableName); yourVariableName;Let us first create an object for our example −> var studentDetails={"StudentName":"Chris","ListOfMarks":[56,78,89],"ListOfSubject":["MySQL","Java","MongoDB"]};Let us save the above created object “studentDetails” −> db.demo40.save(studentDetails); WriteResult({ "nInserted" : 1 })Let us display the value −> studentDetails;This will produce the following output −{ "StudentName" : "Chris", "ListOfMarks" : [ 56, 78, 89 ], "ListOfSubject" : [ "MySQL", "Java", "MongoDB" ], "_id" : ObjectId("5e177757cfb11e5c34d898e2") }
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
260 Views
Let us first create a collection with documents −> db.demo371.insertOne({"Name":"David", "CountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e57f6982ae06a1609a00af2") } > db.demo371.insertOne({"Name":"John", "CountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5e57f69e2ae06a1609a00af3") } > db.demo371.insertOne({"Name":"Bob", "CountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5e57f6a42ae06a1609a00af4") } > db.demo371.insertOne({"Name":"Mike", "CountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e57f6ba2ae06a1609a00af5") }Display all documents from a collection with the help of find() method −> db.demo371.find();This will produce the following output −{ "_id" : ObjectId("5e57f6982ae06a1609a00af2"), "Name" : "David", "CountryName" : "US" } { "_id" : ObjectId("5e57f69e2ae06a1609a00af3"), "Name" : "John", "CountryName" : "UK" } ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
103 Views
At first, switch to a particular database in MongoDB with the USE command as in the below syntax −use yourDatabaseName; db.getCollectionNames();Let us implement the above syntax to display collections of database WEB −> use web; switched to db web > db.getCollectionNames();This will produce the following output −[ "2015-myCollection", "2015-yourCollection", "2019-employeeCollection", "addColumnDemo", "applyConditionDemo", "calculateAverage", "calculateSumOfDocument", "changeSimpleFieldDemo", "check", "checkFieldDemo", "collationExample", "compoundIndexDemo", "countandsumdemo", "creatingAliasDemo", "decreasetimeusingindex", "demo1", "demo10", "demo11", "demo12", "demo13", "demo14", "demo15", "demo16", "demo17", "demo18", ... Read More