XAMPP is an open source, easy to use and easy to install stack that contains Apache webserver, MySQL database, PHP compiler and Perl. MongoDB is one of the most widely NoSQL database in market today. Install MongoDB with Windows XAMPP Posted on November 25, 2017 by pratap470 I found a useful article How to install MongoDB to use with XAMPP on Windows when I struggled with the correct version of phpmongo.dll.
MongoDB is an open source and widely use NoSQL database around. It offers JSON-like documents, ad hoc queries, indexing and real time aggregation which makes the mongoDB so popular in database world.
- Hi there, I am using MacOS Catalina and installed XAMPP on it. When I am trying to install PHP MongoDB driver using following command, it gives me errors mentioned below. Sudo pecl install mongodb I am getting following error: starts wit.
In this tutorial we will see how to download and install mongodb on windows 10 . I will cover all the steps with images so it will easy for you guys to follow.
At the time of writing this tutorial mongodb has 4.0 version. I will install and configure mongodb only. I won’t be installing mongodb compass in this tutorial. I will cover that later.
If you are wondering what is mongodb compass is so it is the official graphical user interface for mongodb which comes with installer. But it doesn’t mean that you cannot install it separately.
So lets start.
Steps: Download and install Mongodb on Windows
- Goto https://www.mongodb.com/download-center#community and download the installation package
- Once the package download, click on it and installer wizard will open.
- Click on Next straight away and it will take to agreement page.
- Accept the agreement by clicking on the checkbox and click on next.
- In choose setup type window, click on complete setup button. Custom type is for advanced users.
- In service configuration window (under complete) you specify optional settings. But do not change anything and click on next
- In this window, wizard will ask if you want to install MongoDB compass or not. If you also want to download MongoDB compass along with mongodb then you can click on this check box. Otherwise uncheck Install MongoDB Compass checkbox.
Note: If you check this checkbox then your installation will take more time.
- Click on Install and you are ready to install mongodb.
- Once installation will complete. Then goto mongodb folder and copy
binfolder directory path which is usually
- Now goto environment variables setting and add copy path. See images below if you feel confusion
- Now open command prompt(cmd) and start mongodb server by typing
mongod. Remember mongod is a command which runs mongodb server. If you want to use mongodb connection in programming language, then you must run this command before.
- Now open another command promt(cmd) and start mongodb shell by typing
show databases;and mongodb list default dbs. Thats it.
Note: Default address of mongodb server is 127.0.0.1 and default port is 27017.
Step by Step Images:
More from WDB24.com
We know MongoDB is very popular open source, document based NoSQL database. In this tutorial I am going to create MongoDB PHP CRUD Example. CRUD means Create, Read, Update and Delete operations. So basically you will add new data (create), fetch data (read), edit existing data (update) and remove existing data (delete) from database.
Apache HTTP Server 2.4, PHP 7.4.3, MongoDB 4.4.0
Setup Project Directory
It’s assumed that you have setup Apache and PHP in Windows system.
Now we will create a project root directory called php-7-mongodb-crud under the Apache server’s htdocs folder.
I may not mention the project root directory in subsequent sections and we will assume that we are talking with respect to the project root directory.
PHP MongoDB Connection
Create a file called mongodb_config.php with the following source code.
Either you can create the collection in the database or if you do not create collection then while you will insert the record MongoDB will automatically create the collection on the fly.
You can use command
db.createCollection(<collection name>, <options>); from the MongoDB command line tool. For our case, I am going to create
users collection using the command
First I switch to my database called roytuts then I try to create the collection but it already exists.
Single Record Insertion
Insert means new record creation in database. I am going to define the following user which we will inset into
users collection in
Now prepare the SQL statements using
BulkWrite() method of MongoDB Driver Manager to insert above user.
Now execute the query to make insertion happen using the following statement:
Once the above statement gets executed, check the users collection in roytuts database using the command
db.users.find(). You should see the following output in the users collection.
So here I have defined single user object and inserted into the
users collection of
roytuts database. Notice I have not defined any fields for users collection but still I was able to insert the data into
users collection because of its NoSQL(Not only Structured Query Language) nature.
Now you may wonder you have not inserted
_id field into the
user1 object then from where this
_id came along with
user1 object? The
_id field is primary key for every document. It’s called
_id and is also accessible via
id and an
_id field is mandatory for an object or document in collection. If an inserted document omits the
_id field, the MongoDB driver automatically generates an ObjectId for the
_id field. The
_id field is the default field for Bson ObjectId’s and it is, by default, indexed.
id are not the same. You may also choose to add a field called
id if you want, but it will not be index unless you add an index.
The ObjectId class is the default primary key for a MongoDB document and is usually found in the
_id field in an inserted document. It looks like, for an example,
An ObjectId is a 12 byte binary BSON type that contain any 12 bytes you want. To be helpful in generating ObjectIds, MongoDB drivers and the server will generate them using a default Algorithm.
Available Records Read
Read all available users from
users collection in
roytuts database using the following piece of code:
nl2br() method is used to convert
'rn' into new line break.
At present we have only one user so you will get only one user in the output.
Executing the above code will give you the following output:
Multiple Records Insertion
Here is the following code snippets that will insert multiple records into MongoDB:
If you check database then you will find three records exist in the collection:
If you run the available record read code example then you will see the following output:
Read Single Record
Read single record from the
users collection in
You will get below output when you run the above code snippets:
$filter: Selects a subset of an array to return based on the specified condition. Returns an array with only those elements that match the condition. The returned elements are in the original order.
$filter has the following syntax:
So we are filtering the record on
first_name of the user. Therefore we have got single record. If you have multiple users with same name then you have to choose unique field or projection for selecting single record.
Update Single Record
Update user where
multi (boolean): Update only the first matching document (multi=false), or all matching documents (multi=true).
upsert (boolean): If filter does not match an existing document, insert a single document. The document will be created from
newObj if it is a replacement document (i.e. no update operators); otherwise, the operators in
newObj will be applied to filter to create the new document.
So here we are first find the record which is having last_name as ‘Roy’ and updating with f
irst_name as ‘Liton’ and
last_name2300 utc to pst. as ‘Sarkar’.
Now check the
users collection in
roytuts database. Notice though we had two users with last name
Roy but only one record got updated because I have updated only one record using
'multi' => false.
Update multiple rows using single query.
Now you can verify the records in database:
Update multiple rows using multiple queries.
Delete Single/Multiple Record(s)
Delete user where
limit (boolean): Delete all matching documents (limit=0), or only the first matching document (limit=1).
You can check the users collection under roytuts database.
Deletion using multiple queries.
Xampp Mongodb Download
Thanks for reading.