Find MongoDB Records with Price Less Than a Specific Value

AmitDiwan
Updated on 15-May-2020 09:16:20

386 Views

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

MongoDB Query to Search for String Like Email in Field Values

AmitDiwan
Updated on 15-May-2020 09:14:43

1K+ Views

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

Count Documents in an Array Based on a Specific Field Value

AmitDiwan
Updated on 15-May-2020 09:12:54

155 Views

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

Count and Sum of Composite Elements in an Array in C++

Sunidhi Bansal
Updated on 15-May-2020 09:11:42

446 Views

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

Set Filtering Conditions for Nested Array in MongoDB

AmitDiwan
Updated on 15-May-2020 09:10:01

1K+ Views

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

Count Numbers Less Than 10^6 Whose Minimum Prime Factor is n in C++

Sunidhi Bansal
Updated on 15-May-2020 09:08:22

222 Views

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

Count Cursor's Iteration in MongoDB

AmitDiwan
Updated on 15-May-2020 09:05:42

203 Views

You need to use custom logic with the help of while loop along with find() cursor. Let us create a collection with documents −> db.demo724.insertOne( ...    { ...       details: ...       { ...          id:101, ...          otherDetails:[ ...             {Name:"Chris"} ...          ] ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab0cce43417811278f5890") } > > > db.demo724.insertOne( ... { ... ... } ... ); {    "acknowledged" : true, ... Read More

Count Common Prime Factors of Two Numbers in C++

Sunidhi Bansal
Updated on 15-May-2020 09:04:26

367 Views

We are given the two numbers let’s say x and y and the task is to find the common prime factors between two numbers. Common prime factors can be found by firstly calculating the common numbers between two numbers and after that checking from the list of common factors the one which are prime numbers.For ExampleInput − x = 10 y = 20 Output − Common prime factor of two numbers are: 2 5Explanation − common primes factors between 10 and 20 are 2 and 5 only.Input − x = 34 y = 12 Output − Common prime factor of ... Read More

Count Array Elements from Specific Field in MongoDB Documents

AmitDiwan
Updated on 15-May-2020 09:02:54

778 Views

To count array elements from a specific field, use $size in MongoDB. Let us create a collection with documents −> db.demo723.insertOne({"Subject":["MySQL", "MongoDB"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab094d43417811278f588a") } > db.demo723.insertOne({"Subject":["C"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab095243417811278f588b") } > db.demo723.insertOne({"Subject":["C++", "Java", "Python"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab095f43417811278f588c") }Display all documents from a collection with the help of find() method −> db.demo723.find();This will produce the following output −{ "_id" : ObjectId("5eab094d43417811278f588a"), "Subject" : [ "MySQL", "MongoDB" ] } { "_id" : ObjectId("5eab095243417811278f588b"), "Subject" : [ "C" ] } { "_id" : ... Read More

Ignore Null Values in MongoDB Documents

AmitDiwan
Updated on 15-May-2020 09:01:54

3K+ Views

To ignore null values in MongoDB, use "$ne" : null in aggregate(). Let us create a collection with documents −> db.demo722.insertOne( ...    { ...       id:101, ...       details: [ ...          { Name:""}, ...          { Name: "David"}, ...          {Name:null}, ...          {Name:"Carol"} ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab07d543417811278f5889") }Display all documents from a collection with the help of find() method −> db.demo722.find();This will produce the following output ... Read More

Advertisements