Php sql to mongodb

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Convert SQL query to mongoDB command

shiranSofer/SQL_to_MONGODB

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Latest Stable Version Total Downloads License

In these materials, you’ll learn about many of the features and productivity tools available in PhpStorm. Examples are navigation, editing, inspections, live templates, refactoring, tools like Composer and the HTTP client, and many more. It’s virtually impossible to cover every option and feature in PhpStorm, but we’re providing a number of practical exercises on how we can do our daily work as PHP developers.

We’ll also cover a vast amount of keyboard shortcuts to make working with PhpStorm more efficient. Other IntelliJ-based IDE’s use the same keyboard shortcuts, so if you know how to work with PhpStorm, you’ll know how to work with WebStorm, RubyMine, PyCharm, IntelliJ IDEA and more. A cheat sheet is available online and is also included in the workshop download.

This workshop is self-paced, meaning you can work your way through exercises on your own, whenever and wherever you want. Exercises come as a PhpStorm project in which every file is a new exercise that may contain code and tips to get things done.

  • Docker for Mac, Docker for Windows or Docker (Linux) 1.13+. See Docker documentation for installation instructions for your operating system.
  • PhpStorm 2016.3+
Читайте также:  Php formatting date and time

There are several ways to get started with the PhpStorm workshop materials:

  • Create a new PhpStorm Workshop Project in PhpStorm
  • Create a new project with Composer. Note that you can also create a new project in PhpStorm: use the Composer project type and search for «jetbrains/phpstorm-workshop» php composer.phar create-project jetbrains/phpstorm-workshop -s dev
  • Clone the project from GitHub git clone https://github.com/JetBrains/phpstorm-workshop.git git checkout docker
  • Download the ZIP wget https://github.com/JetBrains/phpstorm-workshop/archive/master.zip

Most exercises not related to the code editor require having Docker containers running.

  1. Open Settings/Preferences | Build, Execution, Deployment | Docker and select how to connect to the Docker daemon:
    • Windows:
      • Select TCP socket.
      • Set Engine API URL to tcp://localhost:2375.
      • Leave the Certificates folder field empty.

Make sure to enable Expose daemon on tcp://localhost:2375 without TLS in the General section of Docker for Windows settings.

  • Select Docker for Mac
  • Select Unix socket
  • Windows/macOS: use host.docker.internal , which will automatically resolve to the internal address of the host Docker is running on.
  • Linux: execute hostname in Terminal and use the returned value.

There are some things to know about the project:

  • The project can be opened as is in PhpStorm. We’ve included configurations for PHP Remote Interpreter, Database, Deployment Server, PHP Web Debug, PHPUnit and Behat.
  • All numbered folders contain exercises that you can work on. Simply open the numbered files one by one and follow the comments in the file. Most exercises are self-contained, others build on previous exercises.
  • Some of the exercises (like this one) are in Markdown format. You can read these files easier by toggling View to Show Preview Only in the top-right corner.
  • The PhpStorm Reference Card.pdf is the PhpStorm keymap. The latest version can always be found on the PhpStorm website.

Open Source and Contribution

The workshop is Open Source, licensed under the Apache 2 license. If you would like to contribute to the workshop materials, please feel free to fork the repo and send us a pull request. Or if you have a comment, question, or suggestion for improvements, please raise an issue.

About

Convert SQL query to mongoDB command

Источник

Import data from Mysql into MongoDB with PHP

This is meant to be a simple introduction on how to do basic database operations using MongoDB via PHP. 99% of PHP developers use Mysql so I have provided a sample which enables them to see both the types of database operations in code.

MongoDB, for those who are not very familiar, is a high performance NoSQL database which is optimized for document storage. Contrary to popular misconception, NoSQL databases are not exactly a replacement for RDBMS. MongoDB is best where inserts and updates are very frequent or where the data is meant for fast retrieval, but it does not support JOINS or foreign keys to maintain data integrity, so its not good for complex database queries like reporting or analysis.

To find more about MongoDb, check out this link

BASIC TERMS AND CONCEPTS

  • A Database in Mysql is a Database or a set of Collections in MongoDB
  • A table in Mysql is a Collection in MongoDB
  • A row in Mysql is a Document in MongoDB

As you will see in the sample code, all data is in the form of associative arrays. So you can use arrays for all CRUD operations.

INSTALLATION PREREQUISITES

It is assumed you have MongoDB installed in your system. If not then the links below provide information on installing it.

Apart from MongoDB , you will need to make sure the MongoDB extension for PHP is enabled in php.ini. Instructions are given here.

When you run phpinfo() the Mongo extension should show as in the screenshot below:

phpinfo() 2015-08-31 16-35-23

THE CODE

The sample code below reads data from a table called countries in a mysql database called test and then adds the data into a collection called countries in a Mongodb database called testdb.

true, PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES utf8" ); $db = null; // setup mongo db $mongo = new Mongo("localhost"); $mongodb = $mongo->testdb; $ccountries = $mongodb->countries; // open mysql table for reading try < $db = new PDO($dsn, $g_connUserId, $g_connPwd, $options); $sql = "select * from countries"; $stmt = $db->prepare($sql); $stmt->execute(); $rst = $stmt->fetchAll(PDO::FETCH_ASSOC); if (is_array($rst)) < foreach($rst as $row) < // get column data from mysql $countryCode = $row["countryCode"]; $countryName = $row["countryName"]; $currencyCode = $row["currencyCode"]; $population = $row["population"]; $areaInSqKm = $row["areaInSqKm"]; print $countryCode . "," . $countryName . "," . $currencyCode . "," . $population . "," . $areaInSqKm . "
"; // only insert if country is not already present $cursor = $ccountries->findOne(array("countryName"=>$countryName)); if ($cursor == null) < // insert into mongodb collection $collection = array("countrycode"=>$countryCode, "countryName"=>$countryName, "currencyCode"=>$currencyCode, "population"=>$population, "areaInSqKm"=>$areaInSqKm); $ccountries->insert($collection); echo("Added with . $collection["_id"] . "
"); > > > > catch (PDOException $pdex) < exit($pdex->getMessage()); > echo("
"); echo(""); // browse through all the rows in mongodb foreach($ccountries->find() as $collection) < $id = $collection["_id"]; $countryCode = $collection["countrycode"]; $countryName = $collection["countryName"]; $currencyCode = $collection["currencyCode"]; $population = $collection["population"]; $areaInSqKm = $collection["areaInSqKm"]; echo(""); > echo("
CodeNameCurrencyPopulationArea (sq.km)
" . $countryCode . "" . $countryName . "" . $currencyCode . "" . $population . "" . $areaInSqKm . "
"); $mongo->close(); $db = null; ?>

The output of the code is shown below:

2015-08-31 16-44-54

1 Comment

DEPRECATED. Solution uses a very old MongoDB library which is not defunct and no longer in supported.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

MySQL to MongoDB importer

virtimus/mysql2mongo

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

MySQL to MongoDB importer

The scripts are using simple relational to non-relational (r2n) mapping mechanism that should let You automagically convert Your MySQL database into MongoDB set of collections.

The approach is: instead of direct convertion let’s convert MySQL eksport (set of «INSERTS») to MongoDB import (set of «db.updates»).

It would also be interesting to go a little bit further — it should be possible to translate also SELECT’S, UPDATE’S and DELETE’S (using same or similiar r2n mapings) which would make quite nice SQL to NoSQL translator . And this means simplyfing r2n by using the translator on median migration stages. IE: incremental changes could be routed to both MYSQL an MongDB .

wp_users ( `ID` bigint(20) `user_login` `user_pass` . ); wp_usermeta ( `umeta_id` bigint(20) `user_id` bigint(20)  

The mapping(conversion definition):

< _id:1.0, ID:1.0, display_name:"admin", meta:< "admin_color" : "fresh" , "closedpostboxes_ant_elems" : "a:1:" , "comment_shortcuts" : "false" , "description" : "Just testing" , "dismissed_wp_pointers" : "wp330_toolbar,wp330_media_uploader,wp330_saving_widgets" , "first_name" : "John" , "last_name" : "TheAdmin" , "wp_user-settings-time" : "1360265776" , "wp_user_level" : "10" . > user_email:virtimus@gmail.com user_login:admin user_nicename:admin user_pass:$P$BtC.w7H6gOdMRhht5TkBMIWD/xIiAB. user_registered:2012-04-25 06:36:01 . > . 

Let's go through whole process from the begining to the end.

  • get sample MySQL database from https://launchpad.net/test-db/
  • unpack ("tar -xjf" , 7zip etc)
  • run import through mysql commandline tool:
 mysql -u[username] -p[password] -t < employees.sql 

Moving from relational to non-relational in case of salaries/employees/titles seems to be simple and rather obvious: Let's get employees as main collection here and titles and salaries as nested documents:

[ , , // "value" omitted - the default is to place all record data as key:value pairs // "value" omitted - the default is to place all record data as key:value pairs ] 

Now when we have some "r2n" mapping scrap - let's do some action:

  • put above mapping (without comments and empty lines) into file "example/employees.r2n"
  • cd example & export mysql data as full insert statements:
 mysqldump -u[username] -p[password] --skip-extended-insert --complete-insert employees employees salaries titles > employees.sql 
 php r2nconvert.php employees 100 

(convert employees.sql to employees(n).json using mapping employees.r2n each 100k records creating new file)

Notice: You have to have enough RAM memory for parent collections cache (collections which have children) - about 100MB/300k records. (currently implemented as in-memory PHPGENCreator.fieldDataCache) Or You have to partition Your DDL input (in such a way that parent records are inside the same partition as children).

At the end - You should get a set of json files containing updates redy to run on MongoDB:

db.employees.update(, < $set: > ,) db.employees.update(,< $set: > > ,) db.employees.update(,< $set: > > ,) db.employees.update(,< $set: > > ,) . etc 

. and mimport_[name].bat script which gets mongo database name as arg and imports all the collection data to Mongo:

mimport_eployees.bat mymongodb 

Источник

Оцените статью