MongoDB Query to Insert but Limit Total Records

AmitDiwan
Updated on 01-Apr-2020 07:19:11

256 Views

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

Native Querying MongoDB Inside Array and Get the Count

AmitDiwan
Updated on 01-Apr-2020 07:17:50

123 Views

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 Values in MongoDB

AmitDiwan
Updated on 01-Apr-2020 07:14:41

176 Views

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

Query a MongoDB Collection

AmitDiwan
Updated on 01-Apr-2020 07:12:31

329 Views

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

Iterator Invalidation in C++

Ayush Gupta
Updated on 01-Apr-2020 06:47:34

159 Views

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

Makefile in C++ and Its Applications

Ayush Gupta
Updated on 01-Apr-2020 06:46:17

263 Views

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

Kruskal's Minimum Spanning Tree using STL in C++

Ayush Gupta
Updated on 01-Apr-2020 06:43:15

491 Views

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

Lower Bound in C++

Ayush Gupta
Updated on 01-Apr-2020 06:41:03

316 Views

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

Iseek in C/C++ to Read Alternate Nth Byte and Write it in Another File

Ayush Gupta
Updated on 01-Apr-2020 06:38:41

732 Views

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

Internal Details of std::sort in C++

Ayush Gupta
Updated on 01-Apr-2020 06:31:34

255 Views

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

Advertisements