AmitDiwan has Published 8358 Articles
AmitDiwan
417 Views
For this, use update() along with $addToSet. The $addToSet operator adds value to an array unless the value is already present, in which case $addToSet does nothing to that array. Let us create a collection with documents −> db.demo509.insertOne( ... { ... ... "value1" : [ ... ... Read More
AmitDiwan
175 Views
Yes, you can track how many times each index is used in a query using MongoDB $indexStats. Following is the query to create an index in MongoDB −> db.demo508.createIndex({"FirstName":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }Let us create a ... Read More
AmitDiwan
1K+ Views
To filter and fetch, use projection along with MongoDB $filter and $match. Let us create a collection with documents −> db.demo507.insertOne( ... { ... ... "Information": ... [ ... {"Name":"John", "SubjectName":"MySQL"}, ... {"Name":"Bob", "SubjectName":"MongoDB"}, ... {"Name":"Chris", "SubjectName":"MySQL"}, ... ... Read More
AmitDiwan
364 Views
To find the user by name in MongoDB, use find(). Let us create a collection with documents −> db.demo504.insertOne({"Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8823ee987b6e0e9d18f570") } > db.demo504.insertOne({"Name":"John"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8823f2987b6e0e9d18f571") } > db.demo504.insertOne({"Name":"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8823f5987b6e0e9d18f572") }Display ... Read More
AmitDiwan
310 Views
To slice array, use a $slice operator in MongoDB. Let us create a collection with documents −> db.demo503.insertOne({_id:1, Name:"John", Subject:["MySQL", "Java", "C"]}); { "acknowledged" : true, "insertedId" : 1 } > db.demo503.insertOne({_id:2, Name:"David", Subject:["MongoDB", "C++", "Python"]}); { "acknowledged" : true, "insertedId" : 2 }Display all documents from a collection with ... Read More
AmitDiwan
745 Views
For combination, use $concat and check the equality using $eq. Let us create a collection with documents −> db.demo502.insertOne({"FirstName":"John", "LastName":"Smith"});{ "acknowledged" : true, "insertedId" : ObjectId("5e875534987b6e0e9d18f56d") } > db.demo502.insertOne({"FirstName":"David", "LastName":"Miller"});{ "acknowledged" : true, "insertedId" : ObjectId("5e87553e987b6e0e9d18f56e") } > db.demo502.insertOne({"FirstName":"John", "LastName":"Doe"});{ "acknowledged" : true, "insertedId" ... Read More
AmitDiwan
470 Views
To query an array of subdocuments, use $unwind in MongoDB. Let us create a collection with documents −> db.demo499.insertOne({ ... "details": ... [ ... { ... Name :"MIT", ... Rank: 1, ... "CountryName":"US" ... ... Read More
AmitDiwan
451 Views
To group duplicate documents, use MongoDB aggregate(). Let us create a collection with documents −> db.demo501.insertOne({"Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8752f0987b6e0e9d18f566") } > db.demo501.insertOne({"Name":"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8752f4987b6e0e9d18f567") } > db.demo501.insertOne({"Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8752f8987b6e0e9d18f568") } > db.demo501.insertOne({"Name":"John"});{ ... Read More
AmitDiwan
200 Views
For this, use skip() in MongoDB. Under skip(), set “count() – 10” to get 10 most recent documents. Let us create a collection with documents −> db.demo500.insertOne({value:10});{ "acknowledged" : true, "insertedId" : ObjectId("5e8749c5987b6e0e9d18f55a") } > db.demo500.insertOne({value:1200});{ "acknowledged" : true, "insertedId" : ObjectId("5e8749c8987b6e0e9d18f55b") } > db.demo500.insertOne({value:19});{ ... Read More
AmitDiwan
1K+ Views
To retrieve group by result with arrays, use aggregate(). We will also use $addToSet operator. It adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array.Let us create a collection with documents −> db.demo498.insertOne({id:1, Name:["Chris"]});{ "acknowledged" : true, ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP