Nishtha Thakur

Nishtha Thakur

398 Articles Published

Articles by Nishtha Thakur

Page 18 of 40

MongoDB find() to operate on recursive search?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 897 Views

Use find() with dot notation to perform recursive search. Let us first create a collection with documents −> db.findOperationDemo.insertOne({"ClientDetails":[{"ClientId":101, "ClientName":"Chris"}, {"ClientId":102, "ClientName":"Robert"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9a118b50a6c6dd317ad99") } > db.findOperationDemo.insertOne({"ClientDetails":[{"ClientId":110, "ClientName":"David"}, {"ClientId":112, "ClientName":"Mike"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9a12fb50a6c6dd317ad9a") }Following is the query to display all documents from a collection with the help of find() method −> db.findOperationDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd9a118b50a6c6dd317ad99"),    "ClientDetails" : [       {          "ClientId" : 101,          "ClientName" : "Chris"       ...

Read More

MongoDB query to count the size of an array distinctly?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 299 Views

Use DISTINCT for distinct elements and then length to get the size of array −db.yourCollectionName.distinct('yourFieldName').length;Let us first create a collection with documents −> db.countOrSizeDemo.insertOne({"StudentFirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd304f5b64f4b851c3a13dc") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd304fab64f4b851c3a13dd") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd304fcb64f4b851c3a13de") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd30500b64f4b851c3a13df") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd30505b64f4b851c3a13e0") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd3050ab64f4b851c3a13e1") }Following is the query to display all ...

Read More

Hygienic Macros in C

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 630 Views

Here we will see the Hygienic Macros in C. We know the usage of macros in C. But sometimes, it does not return the desired results because of accidental capture of identifiers.If we see the following code, we can see that it is not working properly.Example#include #define INCREMENT(i) do { int a = 0; ++i; } while(0) main(void) {    int a = 10, b = 20;    //Call the macros two times for a and b    INCREMENT(a);    INCREMENT(b);    printf("a = %d, b = %d", a, b); }After preprocessing the code will be like this −Example#include #define ...

Read More

Is there a way to limit the number of records in a certain MongoDB collection?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 135 Views

Yes, you can use capped parameter along with max size. Following is the syntax −db.createCollection("yourCollectionName", {capped:true, size:yourSizeInBytes, max:howManyRecordsYouWant})Let us first create a collection with capped:true −> db.createCollection("limitTheNumberOfRecordsDemo", {capped:true, size:200024, max:3}) { "ok" : 1 }We will now create a collection with documents −> db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"James Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9e601b50a6c6dd317adad") } > db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"Sam Williams"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9e60bb50a6c6dd317adae") } > db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"David Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9e612b50a6c6dd317adaf") } > db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"Carol Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9e61ab50a6c6dd317adb0") } > db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"Adam Smith"}); {    "acknowledged" ...

Read More

Print numbers in sequence using thread synchronization

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 863 Views

Here we will see how to print numbers in a correct sequence using different threads. Here we will create n number of threads, then synchronize them. The idea is, the first thread will print 1, then second thread will print 2 and so on. When one thread is trying to print, it will lock the resource, so no thread can use that portion.Example#include #include #include #include pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t* cond = NULL; int threads; volatile int count = 0; void* sync_thread(void* num) { //this function is used to synchronize the threads    int thread_number ...

Read More

How to keep two “columns” in MongoDB unique?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 1K+ Views

Use unique and set it to TRUE. Let us implement the same by creating index and setting two columns to unique −>db.keepTwoColumnsUniqueDemo.createIndex({"StudentFirstName":1, "StudentLastName":1}, {unique:true}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Now you can insert documents in the above collection −>db.keepTwoColumnsUniqueDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd30fd7b64f4b851c3a13e5") } >db.keepTwoColumnsUniqueDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd30fe5b64f4b851c3a13e6") } >db.keepTwoColumnsUniqueDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith", "StudentAge":24}); 2019-05-08T22:50:42.803+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: sample.keepTwoColumnsUniqueDemo index: StudentFirstName_1_StudentLastName_1 dup key: { : "John", : "Smith" } ...

Read More

Find a strict document that contains only a specific field with a fixed length?\\n

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 131 Views

You can use $where operator for this. Let us first create a collection with documents −>db.veryStrictDocumentDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda4bcdb50a6c6dd317adb8") } > db.veryStrictDocumentDemo.insertOne({"StudentFirstName":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda4bdbb50a6c6dd317adb9") } >db.veryStrictDocumentDemo.insertOne({"StudentFirstName":"David", "StudentLastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda4becb50a6c6dd317adba") } > db.veryStrictDocumentDemo.insertOne({"StudentFirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda4bfbb50a6c6dd317adbb") } > db.veryStrictDocumentDemo.insertOne({"StudentFirstName":"Bob", "StudentLastName":"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda4c6db50a6c6dd317adbc") }Following is the query to display all documents from a collection with the help of find() method −> db.veryStrictDocumentDemo.find();This will produce the following output ...

Read More

Program for Christmas Tree in C

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 1K+ Views

Here we will see one interesting problem. In this problem, we will see how to print Christmas tree randomly. So the tree will be flickering like Christmas tree lights.To print a Christmas tree, we will print pyramids of various sizes just one beneath another. For the decorative leaves a random character is printed from a given list of characters. The height and randomness is adjustable.Here after generating a tree, the whole screen is cleared, then generate again, that’s why this is looking like flickering tree.Example#include #include #include #include #define REFRESH_RATE 40000 #define RANDOM_NESS 5 // The ...

Read More

Project specific array field in a MongoDB collection?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 262 Views

Let us first create a collection with documents −> db.projectionAnElementDemo.insertOne( ...    { ...       "CustomerId":100, ...       "CustomerDetails": [ ...          { ...             "CustomerName": "Chris", ...             "CustomerCountryName": "US" ...          }, ...          { ...             "CustomerName": "Robert", ...             "CustomerCountryName": "UK" ...          } ...       ] ...    } ... ); {    "acknowledged" : true,   ...

Read More

abs(), labs(), llabs() functions in C/C++

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 284 Views

In the cstdlib library of C++, there are different functions for getting the absolute value except from abs. The abs are used basically for int type input in C, and int, long, long long in C++. The others are used for long, and long long type data etc. Let us see the usage of these functions.The abs() FunctionThis function is used for int type data. So this returns the absolute value of the given argument. The syntax is like below.int abs(int argument)Example#include #include #include using namespace std; main() {    int x = -145;    int y = 145;    cout

Read More
Showing 171–180 of 398 articles
« Prev 1 16 17 18 19 20 40 Next »
Advertisements