
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

147 Views
For array concatenation, use $concatArrays operator. Let us first create a collection with documents −>db.arrayConcatenationDemo.insertOne({"TeacherName":["Chris", "Robert"], "StudentName":["Mike", "Sam"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ce921c078f00858fb12e911") }Following is the query to display all documents from a collection with the help of find() method −> db.arrayConcatenationDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5ce921c078f00858fb12e911"), "TeacherName" : [ "Chris", "Robert" ], "StudentName" : [ "Mike", "Sam" ] }Following is the query for array concatenation −> db.arrayConcatenationDemo.aggregate([ { "$project": { ... Read More

2K+ Views
Let us first create a collection with documents −> db.querySelectDemo.insertOne({UserId:100, UserName:"Chris", UserAge:25}); { "acknowledged" : true, "insertedId" : ObjectId("5ce90eb478f00858fb12e90e") } > db.querySelectDemo.insertOne({UserId:101, UserName:"Robert", UserAge:26}); { "acknowledged" : true, "insertedId" : ObjectId("5ce90ec578f00858fb12e90f") } > db.querySelectDemo.insertOne({UserId:103, UserName:"David", UserAge:27}); { "acknowledged" : true, "insertedId" : ObjectId("5ce90ed478f00858fb12e910") }Following is the query to display all documents from a collection with the help of find() method −> db.querySelectDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5ce90eb478f00858fb12e90e"), "UserId" : 100, "UserName" : "Chris", "UserAge" : 25 } { "_id" : ObjectId("5ce90ec578f00858fb12e90f"), "UserId" : 101, ... Read More

437 Views
Let us first create a collection with documents −> db.updateArrayElementDemo.insertOne( { "UserDetails": [ { "UserName":"Chris", "UserAge":23 } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5ce9029378f00858fb12e90d") }Following is the query to display all documents from a collection with the help of find() method −> db.updateArrayElementDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5ce9029378f00858fb12e90d"), "UserDetails" : [ { ... Read More

336 Views
For this, use $elemMatch operator. Let us first create a collection with documents −> db.findDocumentsHaving2Demo.insertOne( {_id : 101, Values: [78, 98]} ); { "acknowledged" : true, "insertedId" : 101 } > db.findDocumentsHaving2Demo.insertOne( {_id :102, Values : [89, 102]} ); { "acknowledged" : true, "insertedId" : 102 }Following is the query to display all documents from a collection with the help of find() method −> db.findDocumentsHaving2Demo.find().pretty();This will produce the following output −{ "_id" : 101, "Values" : [ 78, 98 ] } { "_id" : 102, "Values" : [ 89, 102 ] }Following is the query to find documents ... Read More

795 Views
For sorting and grouping in a single query, use the $group operator along with aggregate framework. Let us first create a collection with documents −> db.sortAndGroupDemo.insertOne({ Price :40, Product: 10 }); { "acknowledged" : true, "insertedId" : ObjectId("5ce8f2bc78f00858fb12e907") } > db.sortAndGroupDemo.insertOne({ Price :100, Product: 10 }); { "acknowledged" : true, "insertedId" : ObjectId("5ce8f2bc78f00858fb12e908") } > db.sortAndGroupDemo.insertOne({ Price :90, Product: 20 }); { "acknowledged" : true, "insertedId" : ObjectId("5ce8f2bc78f00858fb12e909") } > db.sortAndGroupDemo.insertOne({ Price :200, Product: 10 }); { "acknowledged" : true, "insertedId" : ObjectId("5ce8f2bc78f00858fb12e90a") } ... Read More

898 Views
You can use $pull operator for this. Let us first create a collection with documents. Here, we have also added an empty object −> db.removeEmptyObjectsDemo.insertOne( { "_id" :101, "LoginDate" :new ISODate(), "UserDetails" : [ { "UserName" : "John" }, { }, { "UserName" : "Sam" } ] } ); { ... Read More

2K+ Views
To return documents of a collection without objectId, set _id:0. Let us first create a collection with documents −> db.returnDocumentWithoutObjectId.insertOne({"Name":"Carol", "Age":25}); { "acknowledged" : true, "insertedId" : ObjectId("5ce8ba6c78f00858fb12e8fa") } > db.returnDocumentWithoutObjectId.insertOne({"Name":"Sam", "Age":21}); { "acknowledged" : true, "insertedId" : ObjectId("5ce8ba6d78f00858fb12e8fb") } > db.returnDocumentWithoutObjectId.insertOne({"Name":"John", "Age":23}); { "acknowledged" : true, "insertedId" : ObjectId("5ce8ba6f78f00858fb12e8fc") }Following is the query to display all documents from a collection with the help of find() method −> db.returnDocumentWithoutObjectId.find();This will produce the following output −{ "_id" : ObjectId("5ce8ba6c78f00858fb12e8fa"), "Name" : "Carol", "Age" : 25 } { "_id" : ObjectId("5ce8ba6d78f00858fb12e8fb"), "Name" : "Sam", "Age" : ... Read More

96 Views
Yes, to display a database in the list, first create a database and add collection(s), else it won’t be visible in the list. After that, use the SHOW dbs command to display the database name in the list of databases.Following is the query to create a database −> use webcustomertracker; switched to db webcustomertrackerLet us first create a collection with documents −> db.first_Collection.insert({"Name":"Chris"}); WriteResult({ "nInserted" : 1 })Following is the query to display all documents from a collection with the help of find() method −> db.first_Collection.find();This will produce the following output −{ "_id" : ObjectId("5ce2760836e8b255a5eee94a"), "Name" : "Chris" }Following is ... Read More

339 Views
You can’t directly update _id field i.e. write some script to update. Let us first create a collection with documents −> db.updatingIdFieldDemo.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5ce271bb36e8b255a5eee949") }Following is the query to display all documents from a collection with the help of find() method −> db.updatingIdFieldDemo.find();This will produce the following output −{ "_id" : ObjectId("5ce271bb36e8b255a5eee949"), "StudentName" : "Chris" }Following is the query to update _id field in MongoDB −> var myDocument=db.updatingIdFieldDemo.findOne({StudentName:"Chris"}); > myDocument._id = 101; 101 > db.updatingIdFieldDemo.save(myDocument); WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 101 }) > db.updatingIdFieldDemo.remove({_id:ObjectId("5ce271bb36e8b255a5eee949")}); WriteResult({ ... Read More

99 Views
To reduce the time to find record in MongoDB, you can use index. Following is the syntax −db.yourCollectionName.createIndex({yourFieldName:1});You can follow the below approaches to create index for field names based on number, text, hash, etc.First ApproachLet us create an index. Following is the query −> db.takeLessTimeToSearchDemo.createIndex({"EmployeeName":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }Second ApproachTo understand the above concept, let us create another index −> db.takeLessTimeToSearchDemo1.createIndex({"EmployeeName":"text"}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }Third ApproachLet us now create another index −> db.takeLessTimeToSearchDemo2.createIndex({"EmployeeName":"hashed"}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }