MongoDB Create Collection like MySQL CREATE TABLE - php

I am using MongoDB as a replacement for MySQL in Zend Framework 2, I was wondering, is there any CREATE TABLE statement like thing in MongoDB to create collections programmatically, most preferably within ZendFramework 2? Or maybe what is the approach one needs to take while creating DBs and collections when working with MongoDB?

Collections are created automatically in MongoDB as soon as you try to save a document to them.
If you really, really want to have an empty collection, try inserting any document to it to create it and then delete the document. But generally it's best to just let MongoDB do its thing.
I hope this helps.

In MongoDB, a collection is created implicitly when you first insert a document into it. Nevertheless, you can create a collection explicitly with MongoDB by using the command db.createCollection(). This command will also allow you to pass options specifying the nature of the collection, such as whether it is a capped collection, what sort of validation it should have, indexing options, etc. The syntax for MongoDB 3.2 is as follows:
db.createCollection(<name>, { capped: <boolean>,
autoIndexId: <boolean>,
size: <number>,
max: <number>,
storageEngine: <document>,
validator: <document>,
validationLevel: <string>,
validationAction: <string>,
indexOptionDefaults: <document> });
For more information, you can visit this page in the documentation.
Regarding database creation, as of MongoDB 3.2 there is no method to explicitly create a database. So in order to create a database, you need to insert a document to a collection inside it, or create a collection directly using db.createCollection().

Related

Doctrine MongoDB ODM does not persist

I'm trying to start using MongoDB with Doctrine MongoDB ODM 1.1.3 and Laravel 5.4. Everything had been going fine before I removed the database called "doctrine" manually (the default database name I guess) in order to clean up the rubbish in it, so basically I just wanted to remove the database and was hoping that Doctrine will create a new one. Now when I'm trying to call
$mgr->persist($divRoot);
$mgr->flush();
It assigns an ID to the $divRoot object, but doesn't persist it. I.e. when I then call the findAll() method on the repo, it returns nothing. And it doesn't create any databases anymore. The $divRoot has changing fields every time I'm trying to save it. I've got really stuck, please help
UPDATE 1
If I initialize a new DocumentManager specifying a new path to the documents (AnnotationDriver::create($documents)), the ODM works normally persisting and retrieving the documents.
I've figured out what was wrong. I was working only with embedded documents whereas it must be at least one root Document. Originally all of them were marked as Documents that's why I was able to persist them, it's nothing to do with the database removal.
So, I was trying to persist a composite consisting out EmbeddedDocuments only.
The solution was to create a root wrapper for the composite marked as Document.

What kind of entities i should create in doctrine for this db structure

I have two tables in DB (topic, topic_content):
what kind of entities i should create for symfony2?
I think, i should have something like this in my symfony structure (Entities/Topic.php, Entities/Topic_content.php) help me please..
Yes, you would create Topic and Topic Content. And likely also a User Entity (because user_id looks like a foreign key).
However, the idea in Symfony2 is to approach the application from the Model site instead of the database site. Quoting https://doctrine-orm.readthedocs.org/en/latest/tutorials/getting-started-database.html:
Development Workflows
When you Code First, you start with developing Objects and then map them onto your database. When you Model First, you are modelling your application using tools (for example UML) and generate database schema and PHP code from this model. When you have a Database First, you already have a database schema and generate the corresponding PHP code from it.
For database first, there is a generator that will derive objects based on your schema:
https://github.com/beberlei/DoctrineCodeGenerator
The recommended approach though is to have Doctrine generate the db schema from your Entities.
Quoting Getting Started - Generating the DB Schema
Doctrine has a Command-Line Interface that allows you to access the SchemaTool, a component that generates the required tables to work with the metadata.
It requires some setup, which is explained in the guide. Once you have that, you simply tell Doctrine to generate or update your schema, whenever your object structure changes.

MongoDB Rename collection in Sharding

How can I rename a collection in mongodb sharding? Because I tried in my
production server, its thrown the following error just for your references.
My Requirement we have to add few more fields with value on existing collection. But In production we can't able to disrupt the traffic based collection and anytime request comes from member simultaneously. So we have planned to populate the entire records from sql to mongo in another collection and then rename the collection and make it as production. its our plan. But we cant able to take it further. Following error we got it.
db.clientdetails.renameCollection( "clientdetails_bkup" ); {
"assertion" : "You can't rename a sharded collection",
"assertionCode" : 13138,
"errmsg" : "db assertion failure",
"ok" : 0 }
If not possible in mongodb sharding, please share your suggestion or alternative way we can solve this issue.
Please suggest what we have to take it further?.
There is no way to rename a sharded collection.
You can copy all the docs to a new collection.
Or create multiple collections based on a weekly/period date, and use as the current one. Then have your application always use the current one by name and change the name at each period break.
This is one of the worst limitations of MongoDB. we can not rename sharded collection.
Mongodb Document says:
db.collection.renameCollection() is not supported on sharded collections.
We had the same situation where we created another temporary collection, loaded data into that temporary collection. Dropped the existing sharded collection and created new collection with new name and then again loaded back data in newly named collection. This process was very

Rebuild model without loss data in MySQL for Symfony

What is the best way to rebuild a model without loss data in MySQL for Symfony?
What if I have a big site, and a lot of data in the database and I would like after six months to add few new fields to database?
You can use migration.
Doctine manual
Symfony task for migrations
Slideshare presentation
Slideshare presentation
So you need write migrations, migrate, and build your models, forms, etc.
I suggest you use #denys281 for Symfony1.4 ....in Symfony2 however its VERY simple ... just use the command :
php app/console doctrine:schema:update --force
It compares what your database should look like (based on the mapping information of your entities) with how it actually looks, and generates the SQL statements needed to update the database to where it should be. In other words, if you add a new property with mapping metadata to Product and run this task again, it will generate the "alter table" statement needed to add that new column to the existing product table. So it doesnt remove any data
There is also a DoctrineMigrations bundle for Symfony2 if you fancy that route -> http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html

Generate YAML schema or models for Doctrine from MySQL database

Is it somehow possible to automatically generate a YAML schema file or models from an existing MySQL database?
I need to create models for Doctrine but writing the model classes manually seems extremely boring to me. I already have MySQL database with tables and all relations so it would help me if there is some way to generate Doctrine models from it.
If you are using doctrine 2:
http://www.doctrine-project.org/docs/orm/2.0/en/reference/tools.html#reverse-engineering
Yes, it is possible ;-)
For Doctrine 1.2, take a look at the Command Line Interface : amongst other utilities, you have the possibity to generate the YAML files from an existing database.
And, for Doctrine 2.0, you'll want to take a look at Reverse Engineering

Categories