Joomla Jumpstart

  • Increase font size
  • Default font size
  • Decrease font size
Home NoSQL Databases MongoDB How to select a database and collection for MongoDB from PHP

How to select a database and collection for MongoDB from PHP

E-mail Print PDF
User Rating: / 2
PoorBest 

MongoDB logoOnce you have a connection to the MongoDB server, you need to select a database and a collection to access and store data. In PHP, you can select a database and access a collection with two commands.

 

To select a database, use the selectDB() method on the connection object:

$myDB= $myConnection->selectDB($dbName);

To reference a collection, you can simply get a reference to it off the database object:

$myCollectionName = "test_collection";
$myCollection = $myDB->$myCollectionName; 

Note that Mongo will automatically create the referenced database or collection when you access it if it doesn't already exist, so be sure not to make a spelling error.

 
top_java_jobs Social photo sharing website in scala / lift by kramer: Keywords: scala, lift, tomcat, mongodb Th... http://t.co/jHmYqOzm #Java #Jobs
by top_java_jobs. Link: twitterfeed
FriendsOfNodeJS RT @MongoQuestion: "Modelling blogs and ratings in mongodb and nodejs" #MongoDB - http://t.co/7HiR3Cxi
by FriendsOfNodeJS. Link: Mongo Question
danailon RT @michaelklishin: So yes indeed, MongoDB is now just a feature in PostgreSQL 9.2 beta, thanks to just a few functions by @tobyhede https://t.co/SUt9FoGo
by danailon. Link: Twitter for Mac
imjuni RT @kikikiblue: 이번엔 node.js와 mongodb 기반으로 실시간 웹앱을 쉽게 만들어주는 http://t.co/x0IgA2bQ 직접 자신의 서버 배포까지 해결해주는군요.
by imjuni. Link: web

Google AdSense


Coffee and Cream Publishing