Listado de la etiqueta: Find


“Have you ever heard about the data duplication concept while working in databases? When a record, thing, person, or place has its exact copy, that copy is said to be the duplicate of the flamante. While working in traditional databases, we use the WHERE clause to find out the duplicates within the table records, i.e., SQL, PostgreSQL. On the other hand, MongoDB doesn’t allow you to use the WHERE clause to find out the duplicates inserted within the collections of a specific database.

It came up with the aggregate function to find out the duplicate values from the collection. Within this article today, we will be discussing the insertion of duplicate records within the Mongo DB collections and display them on the MongoDB shell using the aggregate command of collections. Let’s start with our today’s article by the use of apt update and upgrade instructions within the terminal shell of the Ubuntu 20.04 system. For that, you need to log in first and open the shell by the use of “Ctrl+Alt+T.” After that, you can try the shown-below instruction at your shell and add the password for the user to continue the update process.”

It might require your confirmation to continue this process. Tap “y” upon asking: “Do you want to continue?”. After that, hit the Enter key.

It may take more or less time to process according to the situation of your system.

After the complete update, you will get the shown-below last lines of processing.

After the successful system update and upgrade, we have to open the MongoDB shell to insert some collections and records within the database. So, we have been using the “mongo” query to do so, as displayed in the image. The shell has been prepared successfully.

While using the “db” instruction at the MongoDB shell, we have found that the “test” database is available for our use.

Therefore, we have been using the “test” database for further queries and creating collection within it. For that, try the “use” instruction followed by the name of a database, i.e., “test.”

To add records, we need a collection in the test database. Thus, we need to create a new collection. For that, we have to try out the “db” instruction along with the “createCollection()” function of MongoDB, followed by the name of a new collection within its parenthesis, i.e., Data. The query was successful, and the collection was created successfully as per the status “ok: 1”. Moreover MongoDB, we tend to utilize the find() function preceded by the collection name to display the records of a specific collection. Therefore, we have tried the “db” instruction followed by the collection name, i.e., Data, and the function find() to do so. The collection “Data” is empty right now. Thus, we need to add some records to the collection.

To insert the records within the Data collection of MongoDB, we need to try out the insert() function within the “db” instruction along with the data in the form of documents, i.e., list format. We have been using a total of 4 columns for the document data of collections, i.e., _id, title, age, and price. We have added a total of 5 records for all these 4 columns of Data collection.

The record was added successfully as per the output above shows the number of records 5 for the “nInserted” option. After this, we will be using the find() function with the “Data” collection to find and display all the records of this collection. We are not passing any arguments to the parenthesis of a find() function to not restrict the collection records. All the 5 records for Data collection have been presented in the Mongo DB shell.

As we have been dealing with the topic of finding the duplicates in the collections of MongoDB, we must have some duplicate records in the collections as well. Therefore, we have been inserting three more records within the Data collection to be used as duplicates of some of the already inserted records. We need to update the “_id” column only as the ID of any column must be unique in MongoDB as we used to do in traditional databases. The same insert function has been used so far with the “Data” collection name. All three records have been added.

Now, when you run the “db” instruction with the collection name “Data” followed by the find() function merienda again on the MongoDB shell, the total of 8 records will be displayed on your screen. We can see the duplicate values for columns other than “_id” in this collection data.

It’s time to try out the aggregate() method for the “Data” collection to list out the specific column values that are duplicated in it. You need to use the shown-below syntax of an aggregate command in MongoDB. The option “$group” is used to add all duplicate values of a specific column in one, while the option $match will be utilized to find out the groups having more than 1 document. On the other hand, the “$project” option will be used to specify the format of showing the duplicate records. The first field of the “$group” option will specify the column name in which we will be searching for duplicates. A total of 3 records have been found duplicated for the column “title” of a Data collection. After this, the same query was tried for the “age” column and got the 3 results again.

Conclusion

The explanation of duplicate records has been given in the introductory paragraph, and we have discussed the difference between finding out the duplicates from traditional databases and MongoDB. For this purpose, we have tried to give an illustration about making a new collection within MongoDB and inserting records within it. Moreover, we have discussed the use of the aggregate function to find out the specific column containing the duplicate value within the collections. This article has displayed the clear difference in finding out the duplicates for MongoDB as a comparison to any other database.



Source link


“While working in the MongoDB database, we tend to utilize the “find” function more often to display the data from the collections as a document. The find() function can be utilized in different ways possible. You can use it to display or restrict the display of the specific number of columns at your output by specifying the column names to 1 or 0, respectively. Along with that, we can also define the multiple conditions within the find() function of MongoDB to restrict the number of records for the collection. Within the SQL database, we try to specify the conditions within the WHERE clause. But, within MongoDB, we need to use other ways. Thus, we have decided to cover those methods in this guide. Let’s start with our new article now. Before diving into the implementation deep down, we have to log in from Ubuntu 20.04 system and open its terminal by the utilization of Ctrl+Alt+T. After opening the shell, it is high time to update our system before going further. These updates are necessary for the smooth implementation of our article. Thus, try the shown below instructions followed by the password of a current user to continue.”

Affirm this action by pressing “y” upon asked. Press Enter to continue. The processing will be displayed on your terminal screen. Within a few seconds, your system will get up to date with the latest versions.

After the update has been completed, we have to launch the MongoDB shell at our Ubuntu 20.04 shell. For this, use the “mongo” keyword command as we did below.

The terminal of MongoDB has been launched and is ready to use. Let’s display the list of available databases of MongoDB in which we want to work via the “show dbs” instruction at its shell area. It will show the total databases available. First, three of them are built-in and used to store configuration data. We will be using the user-defined “test” database in this tutorial. To use the “test” database, try out the “use” instruction with the database name “test.” Press the “Enter” key to execute this instruction.

To try out the multiple conditions within the find() function of MongoDB, we must have some collection in the “test” database and sufficient records within the collection. Right now, our database is empty. Thus, we need to create a new collection from scratch. We need to try out the “createCollection” function within the “db” instruction, followed by the name of a new collection to be created in the parenthesis. We have named the collection “Data.” The query was successful, and the collection was generated successfully as per the “ok: 1” status.

Now, we have a new and empty collection of “Data” within our database. We need to put some values as a MongoDB document in it. To insert the data within the MongoDB collection, we need to try out the db insertion with the insert() function preceded by the collection name. Thus, we have been using the same “db” instruction with our newly created collection name, i.e., Data and the insert() function taking values within it. We have been adding a different number of columns for each document record. The column names are: “_id,” “Name,” “City,” “Age,” “salary,” and “job.” Not every record contains all the columns, as we have mentioned. But, each record must contain the “_id,” “Name,” City,” and “Age” columns within it. A total of 15 records have been added with this insert() function command, as shown.

Before trying the conditions on the Data collection, we will be simply using the “find” function to fetch all its records at merienda on our screen. So, we have tried the find() function within the “db” command of our MongoDB. This command has been displaying all 15 records.

As we have mentioned before, we can restrict the number of columns to be displayed in our MongoDB shell by the use of options 1 and 0 with the column name. So, we will try that as well. We have been restricting the display of column “_id” at the MongoDB shell by setting the column value of “_id” to 0 within the find() function. It displayed all the columns except “_id.”

Let’s use the conditions in the find() function now. Let’s suppose you want to display the only records from the Data collection where the City is “Paris.” For this, you need to specify the “$or” variable, and within its [] brackets, specify the column name with the value “Paris” as we did in the command displayed below. A total of 2 document records have been found so far.

We can also specify the names of the columns to be displayed within the find() instruction as we have done it within the shown instruction so far. A total of 3 records have been found.

Let’s use more than 1 condition for the same column using the “$or” variable in the find() function. So, we have been searching for the records that contain the “job” column value as “Doctor,” “Engineer,” and “ShopKeeper.” We have also specified the columns to be shown. It displayed a total of 3 records.

Apart from column values, you can use the comparison operators as well. We have been using the less than “lt” operator in the find() function to display the records only where the ID is less than 6. It shows a total of 5 records.

Just like that, we have tried the greater than comparison operator for the “salary” column within the find function and got the 3 records in return.

Conclusion

This article is the best help to show you the use of the find() function with multiple conditions to display records of collection within MongoDB. We have tried to cover the most in our illustrations to make you understand how easy it is to do with the find() instruction. We have tried the column values and the comparison operators to limit the number of records or display the specific document records on the shell, i.e., less than, greater than operators.



Source link


“Node.js is a JavaScript environment used as a free server environment that keeps running on the V8 engine and different operating systems like Windows, Linux, MacOS, etc. Because of its solitary essence, Node.js is best suited to non-blocking, task-driven data centers. It was created with genuine, push-based frameworks in the head, which are used for conventional sites and back-end API facilities. We will be using it to connect the MongoDB database with the cloud MongoDB server via the Ubuntu 20.04 Linux system in this article. So, let’s take a look now. Let’s start with the Linux system update for the successful implementation of this article. Thus, we have been trying the sudo apt-get command for update via the sudo rights. It may ask for the password of a currently working Linux user. You have to add the password to proceed.”

After updating your Linux system, you have to install the software-properties-common bundle that contains the set of different repositories used to install different software via the Ubuntu 20.04 shell. To install this, you need to try out this installation command with the apt-get keyword.

After the installation of necessary packages and modules for further installation, we will be moving towards the installation of an “npm” package manager for Nodejs first. To install it, try out the apt-get installation instruction along with the keyword “npm.” Add your password, if asked at the shell, and continue.

Tap “y” to continue moving forward in the processing upon being asked: “Do you want to continue?”.

Wait until the progress reaches 100 percent on the processing of this query. You can also try out the same command with the keyword “nodejs,” as shown below.

After the successful installation of the “npm” package, we will be installing the Nodejs JavaScript environment package on our Ubuntu 20.04 system with the “apt-get” package in the installation instruction. Make certain to indulge the “sudo” privileges to do so. Use the keyword “nodejs” in the command. It will be installed in no more than 2 minutes.

Let’s check out the “npm” version just installed at our end with the version command. So we have been using the keyword “npm” with the “-v” option and got version 6.14.4.

To check if the nodejs has been successfully installed in our system, we need to use the dpkg command with the –get-selections option. It will list down all the mounted packages.

Roll down a little, and you will see “nodejs” in this list.

It’s time to install “MongoDB” at our end with the use of the “npm” package at the shell. So, we have tried the installation command with the keyword “MongoDB” via the “npm” package. Wait until it collects the information and install it fully.

After the installation, it will show the following output, i.e., the installed version of MongoDB. It is also stating that 3 packages might require funding to fully configure them.

You can also try out the “npm” package within the list command along with the keyword “MongoDB” to display the versions of MongoDB installed by the “npm” package. Right now, we have only one version of mongo dB installed on our system, i.e., 4.6.0.

After the successful configurations of npm, nodejs, and mongo dB at the Ubuntu 20.04 Linux system, it’s time to set up the cloud server now. So, you need to open the MongoDB cloud site in your browser and register from the authenticated email. After the successful registration, log in from the cloud MongoDB and create a new cluster. You need to select the location while creating a cluster and label it. For instance, we have named it “test,” as you can see from the “All Clusters” page.

Tap on the cluster name “test” to see its data and servers. You have to make a new user and its password for this cluster “test.” To connect the Linux machine with the cloud server, you need to add the IP address of your Linux machine to the Database Access panel and save it. We have created a user name with “saeed” and given it a new password. You need to create a new database in the cloud as well.

We have created a database “test” and added a new collection “Data” within it. Within the “Data” collection, we have added a single record for columns “id” and “title.” The shown-below output is all about your test server. Tap on the “connect” button shown on your below screen to connect this cloud server with the Linux system via Nodejs. You will be provided with 3 different ways to connect your cloud server with the almacén database, i.e., via terminal shell, via the application, and others. Copy the URL provided to connect a almacén database with the cloud server. Note that the URL will be different according to the Nodejs versions.

Create a new JavaScript file using the touch instruction, i.e., node.js. Add the shown-below code within it. The variable “URL” must contain the copied URL from the cloud. Add your username and password for the “test” server within this URL provided and paste it. This JavaScript code will be used to connect the “test” database of MongoDB with the cloud and display the data of collection “Data” at the shell. If the connection got unsuccessful, it would throw an error.

If you have tried the above code for connectivity, run the node.js file at the shell using the “node” command as shown.

You can also use the URL provided for the option of connecting through the MongoDB shell, as shown.

Conclusion

This tutorial briefly illustrates using the Nodejs to find the MongoDB cloud server data at the almacén server. You need to create an account on a cloud server, generate a new database and its collection, and add a username, password, and Ip address. After that, you need to use the URL provided at your cloud on the MongoDB shell or Nodejs file to connect. The database collection data will be displayed at the shell, i.e., added in the cloud database and shown on the almacén MongoDB shell.



Source link


This article will help you understand various methods we can use to search for a string in a Pandas DataFrame.

Pandas Contains Method

Pandas provide us with a contains() function that allows searching if a substring is contained in a Pandas series or DataFrame.

The function accepts a textual string or a regular expression pattern which is then matched against the existing data.

The function syntax is as shown:

1

Series.str.contains(pattern, case=True, flags=0, na=None, regex=True)

The function parameters are expressed as shown:

  1. pattern – refers to the character sequence or regex pattern to search.
  2. case – specifies if the function should obey case sensitivity.
  3. flags – specifies the flags to pass to the RegEx module.
  4. na – fills the missing values.
  5. regex – if True, treats the input pattern as a regular expression.

Return Value

The function returns a series or index of Boolean values indicating if the pattern/substring is found in the DataFrame or series.

Example

Suppose we have a sample DataFrame shown below:

1
2
3
4
5

# import pandas
import pandas as pd

df = pd.DataFrame({«full_names»: [‘Irene Coleman’, ‘Maggie Hoffman’, ‘Lisa Crawford’, ‘Willow Dennis’,‘Emmett Shelton’]})
df

Search a String

To search for a string, we can pass the substring as the pattern parameter as shown:

1

print(df.full_names.str.contains(‘Shelton’))

The code above checks if the string ‘Shelton’ is contained in the full_names columns of the DataFrame.

This should return a series of Boolean values indicating whether the string is located in each row of the specified column.

An example is as shown:

To get the contemporáneo value, you can pass the result of the contains() method as the index of the dataframe.

1

print(df[df.full_names.str.contains(‘Shelton’)])

The above should return:

1
2

full_names
4  Emmett Shelton

Case Sensitive Search

If case sensitivity is important in your search, you can set the case parameter to True as shown:

1

print(df.full_names.str.contains(‘shelton’, case=True))

In the example above, we set the case parameter to True, enabling a case-sensitive search.

Since we search for the lowercase string ‘shelton,’ the function should ignore the uppercase match and return false.

RegEx search

We can also search using a regular expression pattern. A simple example is as shown:

1

print(df.full_names.str.contains(‘wi|em’, case=False, regex=True))

We search for any string matching the patterns ‘ wi’ or ’em’ in the code above. Note that we set the case parameter to false, ignoring case sensitivity.

The code above should return:

Closing

This article covered how to search for a substring in a Pandas DataFrame using the contains() method. Check the docs for more.



Source link