MongoDB Transaction and Indexes for Duplicate Values

AmitDiwan
Updated on 01-Apr-2020 07:20:37

320 Views

Use ensureIndex() and set unique:1 inside the array. Let us create a collection with documents −> db.demo298.save({Name: 'Chris', Marks: [46, 79] }); WriteResult({ "nInserted" : 1 }) > db.demo298.save({Name: 'David', Marks: [67, 88] }); WriteResult({ "nInserted" : 1 }) > db.demo298.ensureIndex({ Marks: 1 }, {unique: 1}); {    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo298.save({Name: 'Mike', Marks: [88, 98] }); WriteResult({    "nInserted" : 0,    "writeError" : {       "code" : 11000,       "errmsg" : "E11000 duplicate key error collection: test.demo298 index: Marks_1 dup ... Read More

MongoDB Query to Insert but Limit Total Records

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

271 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

143 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

190 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

351 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

173 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

290 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

543 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

349 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

772 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

Advertisements