For sub-documents, use the dot notation. Let us first create a collection with documents −> db.demo537.insertOne({"details":{"SubjectName":"MongoDB"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e8c8a10ef4dcbee04fbbc05") } > db.demo537.insertOne({"details":{"SubjectName":"MySQL"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e8c8a4bef4dcbee04fbbc06") } > db.demo537.insertOne({"details":{"SubjectName":"Java"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e8c8a51ef4dcbee04fbbc07") }Display all documents from a collection with the help of find() method −> db.demo537.find();This will produce the following output −{ "_id" : ObjectId("5e8c8a10ef4dcbee04fbbc05"), "details" : { "SubjectName" : "MongoDB" } } { "_id" : ObjectId("5e8c8a4bef4dcbee04fbbc06"), "details" : { "SubjectName" : "MySQL" } } { "_id" : ObjectId("5e8c8a51ef4dcbee04fbbc07"), "details" : { "SubjectName" : "Java" } ... Read More
For this, use MongoDB aggregate and within that, use $cond. The $cond evaluates a boolean expression to return one of the two specified return expressions.Let us first create a collection with documents −> db.demo536.insertOne({"Name1":"Chris", "Name2":"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8c843eef4dcbee04fbbc01") } > db.demo536.insertOne({"Name1":"David", "Name2":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8c843fef4dcbee04fbbc02") } > db.demo536.insertOne({"Name1":"Bob", "Name2":"Sam"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8c843fef4dcbee04fbbc03") } > db.demo536.insertOne({"Name1":"Chris", "Name2":"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8c843fef4dcbee04fbbc04") }Display all documents from a collection with the help of find() method −> db.demo536.find();This will produce the following output −{ "_id" ... Read More
To delete array values, use $pull in MongoDB. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.Let us first create a collection with documents −> db.demo535.insertOne( ... { ... ... "studentId" : "101", ... "studentName" : "Chris", ... "ListOfMailIds" : [ ... "Chris@gmail.com", ... "Chris@yahoo.com" ... ] ... ... } ... ) { "acknowledged" : true, "insertedId" : ObjectId("5e8c82bfef4dcbee04fbbc00") }Display all documents from a collection with the help of find() method −> db.demo535.find();This will produce the following ... Read More
For this, use $group in MongoDB IN aggregate(). The $group groups input documents by the specified _id expression and for each distinct grouping, outputs a document. Let us first create a collection with documents −> db.demo534.insertOne({_id:10, "ProductId":100, "ProductName":"Product-1"}); { "acknowledged" : true, "insertedId" : 10 } > db.demo534.insertOne({_id:11, "ProductId":100, "ProductName":"Product-2"}); { "acknowledged" : true, "insertedId" : 11 } > db.demo534.insertOne({_id:12, "ProductId":101, "ProductName":"Product-1"}); { "acknowledged" : true, "insertedId" : 12 }Display all documents from a collection with the help of find() method −> db.demo534.find();This will produce the following output −{ "_id" : 10, "ProductId" : 100, "ProductName" : "Product-1" } { ... Read More
The $addToSet operator adds value to an array unless the value is already present, in which case $addToSet does nothing to that array.Let us create a collection with documents −> db.demo533.insertOne({"ProjectName":"Online Hospital Management"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b4cfaef4dcbee04fbbbfc") } > db.demo533.insertOne({"ProjectName":"Online Library Management"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b4d02ef4dcbee04fbbbfd") } > db.demo533.insertOne({"ProjectName":"Online Hospital Management"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b4d04ef4dcbee04fbbbfe") } > db.demo533.insertOne({"ProjectName":"Online Customer Tracker"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b4d0def4dcbee04fbbbff") }Display all documents from a collection with the help of find() method −> db.demo533.find();This will produce the following output −{ ... Read More
For this, use aggregate(). Here, we have considered 3 roles − Admin, Guest, and User. Let us create a collection with documents −> db.demo532.insertOne({"Name":"Chris", "Type":"Admin"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b4a9def4dcbee04fbbbf9") } > db.demo532.insertOne({"Name":"David", "Type":"Guest"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b4aa3ef4dcbee04fbbbfa") } > db.demo532.insertOne({"Name":"Bob", "Type":"User"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8b4ab0ef4dcbee04fbbbfb") }Display all documents from a collection with the help of find() method −> db.demo532.find();This will produce the following output −{ "_id" : ObjectId("5e8b4a9def4dcbee04fbbbf9"), "Name" : "Chris", "Type" : "Admin" } { "_id" : ObjectId("5e8b4aa3ef4dcbee04fbbbfa"), "Name" : "David", "Type" : "Guest" } { "_id" ... Read More
It is possible to have a list whose inner elements are also lists. In such cases we may come across a need when we have to find out the common elements among these inner lists. In this article we will find out the approaches to achieve this.With map and intersectionIntersection is a simple mathematical concept of finding the common elements between different sets. Python has the set method which returns a set that contains the similarity between two or more sets. So we first convert the elements of the list into set through a map function and then apply the ... Read More
During data analysis we face scenarios to convert every element of a list into a sublist. So in this article we will need to take a normal list as an input and convert into a list of lists where each element becomes a sublist.Using for loopThis is a very straight forward approach in which we create for loop to read each element. We read it as a list and store the result in the new list.Example Live DemoAlist = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'] #Given list print("Given list: ", Alist) # Each element as list NewList= [[x] for x ... Read More
Converting from one collection type to another is very common in python. Depending the data processing needs we may have to convert the key value pairs present in a dictionary to pairs representing tuples in a list. In this article we will see the approaches to achieve this.With inThis is a straight forward approach where we just consider theExample Live DemoAdict = {30:'Mon', 11:'Tue', 19:'Fri'} # Given dictionary print("The given dictionary: ", Adict) # Using in Alist = [(key, val) for key, val in Adict.items()] # Result print("The list of tuples: ", Alist)OutputRunning the above code gives us ... Read More
In python data analysis, we may come across situation when we need to compare two lists and find out if they are identical meaning having same elements or not.Exmple Live DemolistA = ['Mon', 'Tue', 'Wed', 'Thu'] listB = ['Mon', 'Wed', 'Tue', 'Thu'] # Given lists print("Given listA: ", listA) print("Given listB: ", listB) # Sort the lists listA.sort() listB.sort() # Check for equality if listA == listB: print("Lists are identical") else: print("Lists are not identical")OutputRunning the above code gives us the following result −Given listA: ['Mon', 'Tue', 'Wed', 'Thu'] Given listB: ['Mon', 'Wed', 'Tue', 'Thu'] Lists are identicalWith ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP