
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 6705 Articles for Database

429 Views
To concatenate, use $concat in MongoDB aggregate(). Let us first create a collection with documents −> db.demo377.insertOne({"ListOfIds":[1001, 1002, 1003, 1004, 1005, 1006, 1007]}); { "acknowledged" : true, "insertedId" : ObjectId("5e5a73462ae06a1609a00b0e") }Display all documents from a collection with the help of find() method −> db.demo377.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e5a73462ae06a1609a00b0e"), "ListOfIds" : [ 1001, 1002, 1003, 1004, 1005, 1006, 1007 ] }Following is the query to concatenate an array of integer ... Read More

644 Views
To update documents, you cannot use aggregation pipeline. You can use update(). Let us first create a collection with documents −> db.demo376.insertOne( ... { ... ... "id" :101, ... ... "details" : [ ... { ... Name:"Chris", ... Age:21, ... Score:45 ... }, ... { ... Name:"David", ... Age:23, ... ... Read More

565 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

307 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

694 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

471 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

403 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

2K+ 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

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

2K+ 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") }