To display a specific field, use $project along with $unwind. To ignore a field, set to 0. Let us create a collection with documents −> db.demo731.insertOne({ "ProductInformation": [ { ProductId:"Product-1", ProductPrice:80 }, { ProductId:"Product-2", ProductPrice:45 }, { ProductId:"Product-3", ProductPrice:50 } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5eac5efd56e85a39df5f6341") }Display all documents from a collection with the help of find() method −> db.demo731.find();This will produce the following output −{ "_id" : ObjectId("5eac5efd56e85a39df5f6341"), "ProductInformation" : [ { "ProductId" : "Product-1", "ProductPrice" : 80 }, { "ProductId" : "Product-2", "ProductPrice" : 45 }, { "ProductId" : "Product-3", "ProductPrice" : ... Read More
To match, use $match in MongoDB. For values greater than a specific number, use $gt. Let us create a collection with documents −> db.demo730.insertOne({"Name" : "Chris", "Marks" : 33 }); { "acknowledged" : true, "insertedId" : ObjectId("5eac54cd56e85a39df5f6339") } > db.demo730.insertOne({ "Name" : "David", "Marks" : 89}); { "acknowledged" : true, "insertedId" : ObjectId("5eac54cd56e85a39df5f633a") } > db.demo730.insertOne({ "Name" : "Chris", "Marks" : 45 }); { "acknowledged" : true, "insertedId" : ObjectId("5eac54ce56e85a39df5f633b") }Display all documents from a collection with the help of find() method −> db.demo730.find();This will produce the following output −{ "_id" : ObjectId("5eac54cd56e85a39df5f6339"), "Name" ... Read More
We are given a string of any length containing both uppercase and lowercase letters and the task is to compute the count of those characters that are at the same position as in english alphabets.For ExampleInput − String str = eBGD Output − Count is: 2Explanation − B and D are the characters that lie in the same order in english alphabets as B comes at second position and D comes at fourth position.Input − String str = Abcdeizxy Output − Count is: 5Explanation − A, B, C, D and E are the characters that lie in the same order ... Read More
For mass insertion, use the concept of insertMany() in MongoDB. The insertMany() inserts multiple documents into a collection.Let us create a collection with documents −> db.demo729.insertMany( [ ... { BankName:"HDFC Bank", cardType:"Credit", "CustomerName":[{Name:"Chris", Age:25}]}, ... { BankName:"ICICI Bank", cardType:"Debit", "CustomerName":[{Name:"Bob", Age:22}]}, ... { BankName:"Kotak Bank", cardType:"Debit", "CustomerName":[{Name:"David", Age:23}]} ... ] ); { "acknowledged" : true, "insertedIds" : [ ObjectId("5eac510d56e85a39df5f6333"), ObjectId("5eac510d56e85a39df5f6334"), ObjectId("5eac510d56e85a39df5f6335") ] }Display all documents from a collection with the help of find() method −> db.demo729.find().pretty();This will produce the following output −{ "_id" : ... Read More
To check the records with Price less than a specific value, use $lt. Let us create a collection with documents −> db.demo728.insertOne({Price:75}); { "acknowledged" : true, "insertedId" : ObjectId("5eab413c43417811278f589b") } > db.demo728.insertOne({Price:59}); { "acknowledged" : true, "insertedId" : ObjectId("5eab414043417811278f589c") } > db.demo728.insertOne({Price:79}); { "acknowledged" : true, "insertedId" : ObjectId("5eab414543417811278f589d") } > db.demo728.insertOne({Price:89}); { "acknowledged" : true, "insertedId" : ObjectId("5eab414843417811278f589e") }Display all documents from a collection with the help of find() method −> db.demo728.find();This will produce the following output −{ "_id" : ObjectId("5eab413c43417811278f589b"), "Price" : 75 } { "_id" : ObjectId("5eab414043417811278f589c"), "Price" : ... Read More
Search for email string using MongoDB find(). Let us create a collection with documents −> db.demo727.insertOne({UserId:"John@email.com"}); { "acknowledged" : true, "insertedId" : ObjectId("5eab375f43417811278f5898") } > db.demo727.insertOne({UserId:"John@yahoo.com"}); { "acknowledged" : true, "insertedId" : ObjectId("5eab376043417811278f5899") } > db.demo727.insertOne({UserId:"Chris@EMAIL.com"}); { "acknowledged" : true, "insertedId" : ObjectId("5eab376143417811278f589a") }Display all documents from a collection with the help of find() method −> db.demo727.find();This will produce the following output −{ "_id" : ObjectId("5eab375f43417811278f5898"), "UserId" : "John@email.com" } { "_id" : ObjectId("5eab376043417811278f5899"), "UserId" : "John@yahoo.com" } { "_id" : ObjectId("5eab376143417811278f589a"), "UserId" : "Chris@EMAIL.com" }Following is the query to search for @email like ... Read More
For such match and count, use $match in MongoDB. Let us create a collection with documents −> db.demo726.insertOne( ... { ... id:101, ... "details": [ ... { ... Name:"Chris" ... ... }, ... { ... Name:"Chris" ... ... }, ... { ... Name:"Bob" ... } ... ] ... ... Read More
We are given with an array of positive integers and the task is to calculate the count and sum of the composite elements in the given array.What are composite numbersFrom the given set of integers, the numbers that are not prime are called composite numbers except 1 which is neither composite nor prime instead it’s a unit number. So, it is clearly stated that a number can be either prime or composite except the number 1.The composite upto 100 are given below −4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, ... Read More
To set filtering conditions, use $filter and $cond in MongoDB aggregate(). The $filter selects a subset of an array to return based on the specified condition. Let us create a collection with documents −> db.demo725.insertOne( ... { ... ... "details": { ... ... "userMessages": [ ... { ... "Messages": [ ... { "Message": "Hello" }, ... { "Message": "How" }, ... ... Read More
We are given a prime number let’s say, num and the task is to calculate the count of all the numbers less than 10^6 whose minimum prime factor is equal to num.For ExampleInput − num = 7 Output − Number of prime factors = 38095 Input − num = 3 Output − Number of prime factors = 16666Approach used in the below program is as followsInput the number let’s say numStart the loop, from i to 2 and i should be less than or equals to max value and increment the value of iInside the loop, check if s_prime[i] ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP