
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

1K+ Views
To find on the basis of true or false values, use $exists in find(). You would also need dot notation for the same task.Let us first create a collection with documents −> db.demo367.insertOne( ... { "Id" : "102", ... "details" : [ { "Name" : "David"}, ... { "Age" : 23, "CountryName" : "UK"} ], ... "isMarried" : false } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e57e0b62ae06a1609a00ae8") } > db.demo367.insertOne( ... { "Id" : "101", ... "details" : [ { "Name" : "Chris", "Subject" : [ "MySQL" ] }, ... ... Read More

202 Views
Use UPDATE to update and SET to set the new values. Let us create a collection with documents −> db.demo34.insertOne({"StudentFirstName":"Chris", "StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5e1758b4cfb11e5c34d898cd") } > db.demo34.insertOne({"StudentFirstName":"David", "StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5e1758bdcfb11e5c34d898ce") } > db.demo34.insertOne({"StudentFirstName":"Bob", "StudentAge":20}); { "acknowledged" : true, "insertedId" : ObjectId("5e1758c6cfb11e5c34d898cf") }Display all documents from a collection with the help of find() method −> db.demo34.find();This will produce the following output −{ "_id" : ObjectId("5e1758b4cfb11e5c34d898cd"), "StudentFirstName" : "Chris", "StudentAge" : 24 } { "_id" : ObjectId("5e1758bdcfb11e5c34d898ce"), "StudentFirstName" : "David", "StudentAge" : 23 } { "_id" : ... Read More

2K+ Views
To sort by length of array, use aggregate(). Before that, get the count of records in an arrayusing $sum. Let us create a collection with documents> db.demo33.insertOne({"ListOfStudent":["Chris", "Bob"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e17556ccfb11e5c34d898ca") } > db.demo33.insertOne({"ListOfStudent":["David", "Adam", "Mike"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e17557acfb11e5c34d898cb") } > db.demo33.insertOne({"ListOfStudent":["Carol", "Sam", "John", "Robert"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e1755a3cfb11e5c34d898cc") }Display all documents from a collection with the help of find() method −> db.demo33.find();This will produce the following output −{ "_id" : ObjectId("5e17556ccfb11e5c34d898ca"), "ListOfStudent" : [ "Chris", "Bob" ] } { "_id" : ObjectId("5e17557acfb11e5c34d898cb"), ... Read More

123 Views
To sort records in a collection, use sort() in MongoDB. Let us create a collection with documents −> db.demo32.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e175158cfb11e5c34d898c5") } > db.demo32.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e17515ccfb11e5c34d898c6") } > db.demo32.insertOne({"Name":"Adam"}); { "acknowledged" : true, "insertedId" : Object0Id("5e175160cfb11e5c34d898c7") } > db.demo32.insertOne({"Name":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e175165cfb11e5c34d898c8") } > db.demo32.insertOne({"Name":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5e17517bcfb11e5c34d898c9") }Display all documents from a collection with the help of find() method −> db.demo32.find();This will produce the following output −{ "_id" : ObjectId("5e175158cfb11e5c34d898c5"), "Name" ... Read More

613 Views
To compare attributes, use $let along with $indexOfArray. Let us first create a collection with documents −> db.demo366.insertOne( ... { ... ... "Name" : "Chris", ... "details" : [ ... { ... "Id" : "John1", ... "value" : "test" ... }, ... { ... "Id" : "John2", ... "value" : 18 ... }, ... { ... "Id" : ... Read More

188 Views
To avoid using index, use hint() in MongoDB. Let us create a collection with documents −> db.demo31.createIndex({"StudentFirstName":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo31.insertOne({"StudentFirstName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e174f8fcfb11e5c34d898c1") } > db.demo31.insertOne({"StudentFirstName":"Jace"}); { "acknowledged" : true, "insertedId" : ObjectId("5e174f97cfb11e5c34d898c2") } > db.demo31.insertOne({"StudentFirstName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e174f9ccfb11e5c34d898c3") } > db.demo31.insertOne({"StudentFirstName":"James"}); { "acknowledged" : true, "insertedId" : ObjectId("5e174fa0cfb11e5c34d898c4") }Display all documents from a collection with the help of find() method −> db.demo31.find();This will produce the following ... Read More

378 Views
For this, use find() and fetch a specific record. Let us create a collection with documents −> db.demo30.insertOne({"City":"New York"}); { "acknowledged" : true, "insertedId" : ObjectId("5e174ceccfb11e5c34d898bd") } > db.demo30.insertOne({"City":"Los Angeles"}); { "acknowledged" : true, "insertedId" : ObjectId("5e174d0ecfb11e5c34d898be") } > db.demo30.insertOne({"City":"Chicago"}); { "acknowledged" : true, "insertedId" : ObjectId("5e174d18cfb11e5c34d898bf") } > db.demo30.insertOne({"City":"Los Angeles"}); { "acknowledged" : true, "insertedId" : ObjectId("5e174d1dcfb11e5c34d898c0") }Display all documents from a collection with the help of find() method −> db.demo30.find();This will produce the following output −{ "_id" : ObjectId("5e174ceccfb11e5c34d898bd"), "City" : "New York" } { "_id" : ObjectId("5e174d0ecfb11e5c34d898be"), "City" : ... Read More

257 Views
To get array items in a MongoDB document, use the dot(.) notation. Let us create a collection with documents −> db.demo29.insertOne({"StudentDetails":[{"StudentName":"Chris", "StudentMarks":58}, {"StudentName":"Bob", "StudentMarks":69}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e15fcc08f2315c2efc48e6e") } >db.demo29.insertOne({"StudentDetails":[{"StudentName":"David", "StudentMarks":97}, {"StudentName":"Carol", "StudentMarks":96}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e15fcd38f2315c2efc48e6f") }Display all documents from a collection with the help of find() method −> db.demo29.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e15fcc08f2315c2efc48e6e"), "StudentDetails" : [ { "StudentName" : "Chris", "StudentMarks" : 58 }, ... Read More

249 Views
Yes, we can use the “.” symbol in MongoDB collection name. Let us create a collection with documents −> db.getCollection('demo28.example'); web.demo28.example > > > db.getCollection('demo28.example').insertOne({"Name":"Chris", "Age":32}); { "acknowledged" : true, "insertedId" : ObjectId("5e15fbe48f2315c2efc48e6b") } > db.getCollection('demo28.example').insertOne({"Name":"Bob", "Age":31}); { "acknowledged" : true, "insertedId" : ObjectId("5e15fbec8f2315c2efc48e6c") } > db.getCollection('demo28.example').insertOne({"Name":"David", "Age":33}); { "acknowledged" : true, "insertedId" : ObjectId("5e15fbf38f2315c2efc48e6d") }Display all documents from a collection with the help of find() method −> db.getCollection('demo28.example').find().pretty();This will produce the following output −{ "_id" : ObjectId("5e15fbe48f2315c2efc48e6b"), "Name" : "Chris", "Age" : 32 } { "_id" : ObjectId("5e15fbec8f2315c2efc48e6c"), "Name" : ... Read More

339 Views
Yes, it is possible using the UPDATE() method. Let us create a collection with documents −> db.demo27.insertOne({"StudentDetails":{"101":{"Subject":["Java"]}}}); { "acknowledged" : true, "insertedId" : ObjectId("5e15f9e822d07d3b95082e7f") } > db.demo27.insertOne({"StudentDetails":{"101":{"Subject":["MySQL"]}}}); { "acknowledged" : true, "insertedId" : ObjectId("5e15f9eb22d07d3b95082e80") }Display all documents from a collection with the help of find() method −> db.demo27.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e15f9e822d07d3b95082e7f"), "StudentDetails" : { "101" : { "Subject" : [ "Java" ] } } } { ... Read More