SELECT * FROM $ category

I want to do a request which is in mysql like SELECT * FROM '.$category.' <-- the from table is random and its given over the POST. How do I do this in MongoDB?

I started like:

$db->$category->

$category = $_POST['category'];
$category = $liste[$category];

Can anyone please help me? btw. :

Performance Problem:

Is it faster of any reason to do more then one collection in mongodb? For example i put 10 collections with each 1 million in it or 1 with 10 million?

Kind Regards

$db->selectCollection( $category )->find();

That will give you all of the results from a certain collection as a MongoCursor object. Not sure what else you are looking for.

Is it faster of any reason to do more then one collection in mongodb? For example i put 10 collections with each 1 million in it or 1 with 10 million?

As with many databases, MongoDB can scale horizontally, not only that but MongoDBs internals on these matters prefer to work over a DB level (i.e. it has a DB level lock, read lock is concurrent though).

This is a classic question of: how to scale a database?

This is a topic that is vast and almost untangeable in scale and breadth. I recommend you first do a Google search for some reason on "database scaling" or more specifically "mongodb scaling".

Both MongoDB and MySQL could easily handle 10 million records in one collection and separating them out would likely give absolutely no performance gain what-so-ever.

You would be better off just having two collections:- post and category whereby you select a post via the _id of the category document like so:

$db->post->find(array('category' => new MongoId('blah')));

I suppose if you were to get an insane (border line stupid) amount of concurrent writes and you was to saturate the lock on a single database you would split collection(s) (data) into separate databases, however, I would highly doubt you ever need that.

I recommend to you take just a few minutes to read the basics of the mongodb php extension http://www.php.net/manual/en/mongocollection.find.php

It's easy to get started and you can follow the examples.

Regarding the performance, it will depends a lot on your setup.

I have created two collections and filled with demo data:

one million:

for (var i=0; i<100000; i++) { cat = { name : 'category'+i }; db.onemillion.save(cat); }

ten million:

for (var i=0; i<10000000; i++) { cat = { name : 'category'+i }; db.tenmillion.save(cat); }

On my setup/hardware these are the results querying the category name:

one million:

> db.onemillion.find({'name':'category12345'}).explain();
{
    "cursor" : "BasicCursor",
    "isMultiKey" : false,
    "n" : 1,
    "nscannedObjects" : 1000000,
    "nscanned" : 1000000,
    "nscannedObjectsAllPlans" : 1000000,
    "nscannedAllPlans" : 1000000,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 1,
    "nChunkSkips" : 0,
    "millis" : 331,
    "indexBounds" : {

    },
    "server" : "station7.local:27017"
}
>

ten million:

> db.tenmillion.find({'name':'category12345'}).explain();
{
    "cursor" : "BasicCursor",
    "isMultiKey" : false,
    "n" : 1,
    "nscannedObjects" : 10000000,
    "nscanned" : 10000000,
    "nscannedObjectsAllPlans" : 10000000,
    "nscannedAllPlans" : 10000000,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 4,
    "nChunkSkips" : 0,
    "millis" : 3301,
    "indexBounds" : {

    },
    "server" : "station7.local:27017"
}
> 

As expected.

You can use it to test your case.

I can't imagine an use case of a ten million collection of categories.

So if you can provide more information, would be better.