Count Cursor's Iteration in MongoDB

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

217 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

396 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

802 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

Count Fibonacci Numbers in Given Range in O(log n) Time and O(1) Space in C++

Sunidhi Bansal
Updated on 15-May-2020 09:00:55

637 Views

We are given the range having start and end numbers and the task is to calculate the total count of Fibonacci numbers available between the given range in O(Log n) time and O(1) space.What are Fibonacci numbersFibonacci numbers are the sequence of numbers known as Fibonacci sequence where every new number is the sum of the last two preceding numbers.Where, f(0) = 0 and f(1) = 1 i.e. f(0) and f(1) have fixed positions in the sequence and the calculation will start from the third number.Formula used for calculating the sequence is −Fn = Fn-1 + Fn-2Where, F0 = 0, ... Read More

Count by Multiple Fields with MongoDB Aggregation

AmitDiwan
Updated on 15-May-2020 08:59:09

847 Views

To count by multiple fields, use $facet in MongoDB. The $facet processes multiple aggregation pipelines within a single stage on the same set of input documents. Let us create a collection with documents −> db.demo721.insertOne( ...    { ... ...       "details1": { ...          "id":101 ... ...       }, ...       "details2": { ...          "id":101 ...       }, ...       "details3": { ...          "id":101 ...       } ...    } ... ); {    "acknowledged" : ... Read More

Update All Values of a Field with a Specific String in MongoDB

AmitDiwan
Updated on 15-May-2020 08:55:19

279 Views

To update all the values, use update() along with multi:true. Let us create a collection with documents −> db.demo720.insertOne({"SubjectName":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eaae7ca43417811278f5883") } > db.demo720.insertOne({"SubjectName":"Java"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eaae7ce43417811278f5884") } > db.demo720.insertOne({"SubjectName":"C"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eaae7d143417811278f5885") } > db.demo720.insertOne({"SubjectName":"C++"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eaae7d543417811278f5886") }Display all documents from a collection with the help of find() method −> db.demo720.find();This will produce the following output −{ "_id" : ObjectId("5eaae7ca43417811278f5883"), "SubjectName" : "MySQL" } { "_id" : ObjectId("5eaae7ce43417811278f5884"), "SubjectName" : "Java" } { ... Read More

Add New Field and Concatenate Price in MongoDB Query

AmitDiwan
Updated on 15-May-2020 08:52:31

451 Views

To add a new field, use the $addFields in MongoDB. Let us create a collection with documents −> db.demo719.insertOne( ...    { ...       "Number":"7374644", ...       "details" : { ...          "otherDetails" : [ ...             { ...                "ProductId" :"102", ...                "ProductPrice" : NumberInt(500) ...             }, ...             { ...                "ProductId" :"103", ...     ... Read More

Count Digits in a Factorial in C++

Sunidhi Bansal
Updated on 15-May-2020 08:51:41

2K+ Views

We are given an integer value and the task is to first calculate the factorial of a number and then calculate the total number of digits in a result.What is a factorial numberFactorial of a number is calculated by multiplying the digits in a number while decrementing the value of digit by 1. It is denoted by the symbol ‘!’ i.e. 0!, 1!, 2!, 3!, 5!, ...., etc. Factorial of 0! and 1! is always 1.I.e. factorial of 2 = 2 * (2-1) = 2 * 1 = 2       factorial of 3 = 3 * (3-1) * ... Read More

Count Factorial Numbers in a Given Range in C++

Sunidhi Bansal
Updated on 15-May-2020 08:49:16

387 Views

We are given the range starting from an integer value holded by a variable let’s say start till the variable end and the task is to count the total number of factorial numbers available in the given range.What is a factorial numberFactorial of a number is calculated by multiplying the digits in a number while decrementing thevalue of digit by 1. It is denoted by the symbol ‘!’ i.e. 0!, 1!, 2!, 3!, 5!, ...., etc. Factorial of 0! and 1! is always 1.I.e. factorial of 2 = 2 * (2-1) = 2 * 1 = 2       ... Read More

Advertisements