Prevent Duplicates of Multiple Fields with Index in MongoDB

AmitDiwan
Updated on 31-Mar-2020 08:52:18

482 Views

To prevent duplicates of multiple fields, use ensureIndex() and set unique:true. Let us create a collection with documents −> db.demo272.ensureIndex({"FirstName":1, "Subject":1}, {unique:true}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo272.insertOne({"FirstName":"Chris", "Subject":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48232a1627c0c63e7dbabf") } > db.demo272.insertOne({"FirstName":"Chris", "Subject":"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48232f1627c0c63e7dbac0") } > db.demo272.insertOne({"FirstName":"David", "Subject":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48233a1627c0c63e7dbac1") } > db.demo272.insertOne({"FirstName":"Chris", "Subject":"MySQL"}); 2020-02-15T22:28:55.137+0530 E QUERY    [js] WriteError: E11000 duplicate key error collection: test.demo272 index: FirstName_1_Subject_1 dup key: { : ... Read More

Increment Value Using Custom Variable in MongoDB Query

AmitDiwan
Updated on 31-Mar-2020 08:50:12

447 Views

Set a custom variable and use update() along with $inc to increment. Let us create a collection with documents −> db.demo271.insertOne({"Marks":56}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4821211627c0c63e7dbabc") } > db.demo271.insertOne({"Marks":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4821241627c0c63e7dbabd") } > db.demo271.insertOne({"Marks":72}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48212b1627c0c63e7dbabe") }Display all documents from a collection with the help of find() method −> db.demo271.find();This will produce the following output −{ "_id" : ObjectId("5e4821211627c0c63e7dbabc"), "Marks" : 56 } { "_id" : ObjectId("5e4821241627c0c63e7dbabd"), "Marks" : 78 } { "_id" : ObjectId("5e48212b1627c0c63e7dbabe"), "Marks" : 72 }Following is the query ... Read More

Select Multiple Values with MongoDB OR Operator

AmitDiwan
Updated on 31-Mar-2020 08:48:45

735 Views

Let us first create a collection with documents −> db.demo270.insertOne({"ClientName":"Chirs", "Age":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e481e371627c0c63e7dbab8") } > db.demo270.insertOne({"ClientName":"David", "Age":31}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e481e3d1627c0c63e7dbab9") } > db.demo270.insertOne({"ClientName":"Bob", "Age":31}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e481e431627c0c63e7dbaba") } > db.demo270.insertOne({"ClientName":"Carol", "Age":36}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e481e491627c0c63e7dbabb") }Display all documents from a collection with the help of find() method −> db.demo270.find();This will produce the following output −{ "_id" : ObjectId("5e481e371627c0c63e7dbab8"), "ClientName" : "Chirs", "Age" : 34 } { "_id" : ObjectId("5e481e3d1627c0c63e7dbab9"), "ClientName" : "David", "Age" : 31 } ... Read More

List All Classes, Interfaces, and Enums in JShell in Java 9

raja
Updated on 31-Mar-2020 08:47:01

460 Views

The JShell tool also called REPL(Read-Evaluate-Print-Loop) introduced in Java 9 that allows us to execute Java code and getting immediate results. We can quickly evaluate expressions or short algorithms without creating a new project, compile or build it. With the help of JShell, we can execute expressions, use imports, define classes, methods, and variables.We can list out all the classes, interfaces and enums defined in the current JShell session by using "/types" command.In the below code snippet, we have created the "Test" class, "TestInterface" interface, and enum "EnumTest" in the JShell tool.C:\Users\User> jshell | Welcome to JShell -- Version 9.0.4 | For ... Read More

Select Special Fields Rather Than All in MongoDB

AmitDiwan
Updated on 31-Mar-2020 08:46:24

152 Views

For this, simply use find(). Set the fields you don’t want to select to 0. Let us create a collection with documents −> db.demo269.insertOne({StudentId:101, StudentSubject:"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e481caa1627c0c63e7dbab4") } > db.demo269.insertOne({StudentId:102, StudentSubject:"Java"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e481cb11627c0c63e7dbab5") } > db.demo269.insertOne({StudentId:103, StudentSubject:"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e481cb21627c0c63e7dbab6") } > db.demo269.insertOne({StudentId:104, StudentSubject:"C"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e481cb21627c0c63e7dbab7") }Display all documents from a collection with the help of find() method −> db.demo269.find();This will produce the following output −{ "_id" : ObjectId("5e481caa1627c0c63e7dbab4"), "StudentId" : 101, "StudentSubject" ... Read More

Search Value in Object with Number Keys in MongoDB

AmitDiwan
Updated on 31-Mar-2020 08:45:09

215 Views

To search a value, simply use $where in MongoDB. Let us create a collection with documents −> db.demo268.insertOne( ...   { ...      "details" : { ...         "101" : "John", ...         "1001" : "Bob" ...      } ...   } ...); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4816141627c0c63e7dbaaf") }Display all documents from a collection with the help of find() method −> db.demo268.find();This will produce the following output −{ "_id" : ObjectId("5e4816141627c0c63e7dbaaf"), "details" : { "101" : "John", "1001" : "Bob" } }Following is the query to search a ... Read More

Use MongoDB Aggregate to Select Top Record in Descending Order

AmitDiwan
Updated on 31-Mar-2020 08:42:34

246 Views

For descending order, use -1, which specifies sorting order for sort(), Let us create a collection with documents −> db.demo267.insertOne({id:100, "Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4811951627c0c63e7dbaab") } > db.demo267.insertOne({id:100, "Name":"Adam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48119e1627c0c63e7dbaac") } > db.demo267.insertOne({id:100, "Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4811a51627c0c63e7dbaad") } > db.demo267.insertOne({id:100, "Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4811ab1627c0c63e7dbaae") }Display all documents from a collection with the help of find() method −> db.demo267.find().pretty(); {    "_id" : ObjectId("5e4811951627c0c63e7dbaab"),    "id" : 100,    "Name" : "Chris" } {    "_id" ... Read More

Query with NAND Operator in MongoDB

AmitDiwan
Updated on 31-Mar-2020 08:39:55

267 Views

The $not operator won’t invert a complex expression. Therefore, use $and or $or with $ne operator.Let us create a collection with documents −> db.demo266.insertOne({"active1":true, "active2":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e480f4b1627c0c63e7dbaa7") } > db.demo266.insertOne({"active1":true, "active2":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e480f501627c0c63e7dbaa8") } > db.demo266.insertOne({"active1":false, "active2":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e480f561627c0c63e7dbaa9") } > db.demo266.insertOne({"active1":false, "active2":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e480f701627c0c63e7dbaaa") }Display all documents from a collection with the help of find() method −> db.demo266.find();This will produce the following output −{ "_id" : ObjectId("5e480f4b1627c0c63e7dbaa7"), "active1" : true, "active2" ... Read More

Remove K Digits in C++

Arnab Chakraborty
Updated on 31-Mar-2020 08:38:57

737 Views

Suppose we have a non-negative integer num that is represented as a string, we have to remove k digits from the number so that the new number is the smallest possible. So if the input is like “1432219” and k = 3, then the result will be “1219”.To solve this, we will follow these steps −Define a stack st, create an empty string retn := size of numfor i in range 0 to n – 1while k is non zero and stack is not empty and top of stack > num[i]delete from the stack and decrease k by 1insert num[i] ... Read More

MongoDB Query to Update Only Certain Fields

AmitDiwan
Updated on 31-Mar-2020 08:37:55

251 Views

To update only certain fields, use $set. Let us create a collection with documents −> db.demo265.insertOne({"id":101, "Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e480d781627c0c63e7dbaa4") } > db.demo265.insertOne({"id":102, "Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e480d7d1627c0c63e7dbaa5") } > db.demo265.insertOne({"id":103, "Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e480d841627c0c63e7dbaa6") }Display all documents from a collection with the help of find() method −> db.demo265.find();This will produce the following output −{ "_id" : ObjectId("5e480d781627c0c63e7dbaa4"), "id" : 101, "Name" : "Chris" } { "_id" : ObjectId("5e480d7d1627c0c63e7dbaa5"), "id" : 102, "Name" : "Bob" } { "_id" : ObjectId("5e480d841627c0c63e7dbaa6"), "id" : 103, ... Read More

Advertisements