To insert and limit the total records while inserting, use capped:true and set the size and max values.Let us create a collection with documents wherein we have set capped:true and size to 4 −> db.createCollection("demo297", {capped:true, size:4, max:4}); { "ok" : 1 } > db.demo297.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d54385d93261e4bc9ea43") } > db.demo297.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d543e5d93261e4bc9ea44") } > db.demo297.insertOne({"Name":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d543e5d93261e4bc9ea45") } > db.demo297.insertOne({"Name":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d543f5d93261e4bc9ea46") } > db.demo297.insertOne({"Name":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d54405d93261e4bc9ea47") ... Read More
To query inside array and check for existence to get the count, use $exists. Let us create a collection with documents −> db.demo296.insertOne( ... { ... "id":101, ... "Name":"Chris", ... "details":[ ... { ... SubjectId:[101, 103], ... "SubjectName":["MySQL", "MongoDB"] ... }, ... { ... SubjectId:[102, 104], ... "SubjectName":["Java", "C"] ... } ... ] ... ... Read More
Query object’s field array value using arrayFieldName along with value. Let us create a collection with documents −> db.demo295.insertOne({"status":["Active", "Inactive"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d4ea65d93261e4bc9ea39") } > db.demo295.insertOne({"status":["Yes", "No"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d4eb15d93261e4bc9ea3a") }Display all documents from a collection with the help of find() method −> db.demo295.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e4d4ea65d93261e4bc9ea39"), "status" : [ "Active", "Inactive" ] } { "_id" : ObjectId("5e4d4eb15d93261e4bc9ea3a"), "status" : [ "Yes", "No" ] ... Read More
To query or return a MongoDB collection, use getCollection(). Let us create a collection with documents −> db.demo294.insertOne({"EmployeeId":101, "EmployeeName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d4a1a5d93261e4bc9ea36") } > db.demo294.insertOne({"EmployeeId":102, "EmployeeName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d4a255d93261e4bc9ea37") } > db.demo294.insertOne({"EmployeeId":103, "EmployeeName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d4a335d93261e4bc9ea38") }Display all documents from a collection with the help of find() method −> db.demo294.find();This will produce the following output −{ "_id" : ObjectId("5e4d4a1a5d93261e4bc9ea36"), "EmployeeId" : 101, "EmployeeName" : "Chris" } { "_id" : ObjectId("5e4d4a255d93261e4bc9ea37"), "EmployeeId" : 102, "EmployeeName" : "Bob" } { "_id" : ObjectId("5e4d4a335d93261e4bc9ea38"), "EmployeeId" ... Read More
In this tutorial, we will be discussing a program to understand iterator invalidation in C++.While iterating over the elements of a container object, sometimes it can become invalidated if we don’t apply bound checks. This mainly occurs due to the change in the shape and size of the container object.Example Live Demo#include using namespace std; int main() { //declaring a vector vector v{1, 5, 10, 15, 20}; //changing vector during execution //which will cause bound invalidation for (auto it=v.begin();it!=v.end();it++) if ((*it) == 5) v.push_back(-1); for (auto ... Read More
In this tutorial, we will be discussing a program to understand MakeFile in C++ and its applications.The task is to break the entire program with MakeFile. It is usually done by making .cpp files and .h files with all the classes/functionalities and link them together.Examplemain.cpp#include #include "function.h" using namespace std; //main execution program int main(){ int num1 = 1; int num2 = 2; cout
In this tutorial, we will be discussing a program to understand Kruskal’s minimum spanning tree using STL in C++.For this, we will be provided with a connected, undirected and weighted graph. Our task is to calculate the Minimum spanning tree for the given graph.Example Live Demo#include using namespace std; typedef pair iPair; //structure for graph struct Graph{ int V, E; vector< pair > edges; Graph(int V, int E){ this->V = V; this->E = E; } void addEdge(int u, int v, int w){ edges.push_back({w, {u, v}}); } ... Read More
In this tutorial, we will be discussing a program to understand the lower bound in C++.lower_bound() method in C++ is used to return the very first number in the container object which is not less than the given value.Example Live Demo#include int main(){ std::vector v{ 10, 20, 30, 40, 50 }; std::cout
In this tutorial, we will be discussing a program to understand how to read the alternate nth byte and write it in another file.For this, we will be provided with two .txt files. Our task is to write the contents from one file to another file using Iseek() which is used to change the pointer of the file descriptor.Example#include #include #include #include void func(char arr[], int n){ int f_write = open("start.txt", O_RDONLY); int f_read = open("end.txt", O_WRONLY); int count = 0; while (read(f_write, arr, 1)){ if (count < n) ... Read More
In this tutorial, we will be discussing a program to understand the internal details of std::sort() in C++.std::sort() function is used to sort down an array using a comparison of the elements. If we look at the in-depth functionality of std::sort() it uses IntroSort algorithm to sort the elements of a container object.Example Live Demo#include using namespace std; int main(){ int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0}; int n = sizeof(arr)/sizeof(arr[0]); sort(arr, arr+n); cout