Home > Backend Development > PHP Tutorial > PHP operation mongoDB instance analysis, phpmongodb instance analysis_PHP tutorial

PHP operation mongoDB instance analysis, phpmongodb instance analysis_PHP tutorial

WBOY
Release: 2016-07-13 10:10:32
Original
999 people have browsed it

php operation mongoDB instance analysis, phpmongodb instance analysis

The example in this article describes how to operate mongoDB in PHP. Share it with everyone for your reference. The specific analysis is as follows:

The mongoDB database is a database stored in json format, which is very suitable for various application development. Here I will introduce some mongoDB learning examples to my friends.

If mongodb wants to integrate PHP, you need to install the Mongo extension. This is relatively simple. Now let’s talk about MongoDB PHPAPI and its usage.

Let’s look at a simple example first. The example code is as follows:

Copy code The code is as follows:
$m = new Mongo(); //The default port 27017 is used to connect to the local machine. Of course, you can also connect to a remote host such as 192.168.0.4:27017. If the port is 27017, the port can be omitted
$db = $m -> comedy; // Select comedy database. If the database has not been created before, it will be automatically created. You can also use $m->selectDB("comedy");
$collection = $db->collection; //Select the collection in comedy, which is equivalent to the table in RDBMS. You can also use
$db->selectCollection("collection");
$obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );
$collection->insert($obj); //Add $obj to the $collection collection
$obj = array( "title" => "XKCD", "online" => true );
$collection->insert($obj);
$cursor = $collection->find();
foreach ($cursor as $obj) { //Traverse all documents in the collection
echo $obj["title"] . "n";
}
$m->close(); //Disconnect MongoDB connection

Below are some commonly used functions. The Php code is as follows:
Copy code The code is as follows:
$query = array( "i" => 71 );
$cursor = $collection->find( $query );// Find documents that satisfy $query in the $collectio collection
while( $cursor->hasNext() ) {
var_dump( $cursor->getNext() );
}

$collection -> findOne();//Return the first document in the $collection collection
$collection -> count(); //Return the number of documents in the $collection collection
$coll->ensureIndex( array( "i" => 1 ) ); // Add index to i "this column" in descending order
$coll->ensureIndex( array( "i" => -1, "j" => 1 ) ); // Add index to i "this column" in descending order j ascending order

When querying, each Object will automatically generate a unique _id when inserted, which is equivalent to the primary key in RDBMS. It is very convenient for querying. The Php code is as follows:
Copy code The code is as follows:
$person = array("name" => "joe");
$people->insert($person);
$joe = $people->findOne(array("_id" => $person['_id']));
?>

When updating: If we want to modify the name of the author in the comments in the document below, the Php code is as follows:
Copy code The code is as follows:
{
"_id" : ObjectId("4b06c282edb87a281e09dad9"),
"content" : "this is a blog post.",
"comments" :
[
             {
"author" : "Mike",
"comment" : "I think that blah blah blah...",
         },
             {
"author" : "John",
"comment" : "I disagree."
         }
]
}

In order to change an internal field, we use $set to ensure that other fields in the document are not removed, and the index of comment is also changed. The Php code is as follows:
Copy code The code is as follows:
$collection->update($criteria, array('$set' => array("comments.1" => array("author" => "Jim")))); //$criteria is Element to update
?>

To delete a database, the Php code is as follows:
Copy code The code is as follows:
$m -> dropDB("comedy");

List all available databases, the Php code is as follows:

Copy code The code is as follows:
$m->listDBs(); //No return value

Okay, that’s it for now. If you are interested, you can search for other usage of Mongo-php API on the Internet.

Command line usage example:

1. db.system.users.find()

2. db.users.count()

3. db.users.ensureIndex({password:-1})

4. use test

5. db.users.getIndexes()

6. db.repairDatabase()

7. show users

8. show dbs

9. db.users.find({username:{$in:['4d81a82398790']}}).explain()

10. db.users.dropIndexes()

11. db.users.find().count()

12. db.users.find().limit(5)

13. db.users.find({"username":"ssa"})

14. show collections

15. db.users.remove()

16. db.user.remove({'username':'admin'})

17. db.user.insert({'username':'admin','age':21,'nickname':'admin'})

18. db.user.save({'username':'admin','age':21,'info':['12','12313','zzsd']})

19. db.createCollection("user")

20. db.dropDatabase()

21. show collections

22. db.test.drop()

23. db.copyDatabase('test','test1')

24. show profile

25. db.printCollectionStats()

26. db.addUser('admin','admin123')

27. db.setProfilingLevel(2);

28. db.setProfilingLevel( 1 , 10 );

29. db.system.profile.find()

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/934929.htmlTechArticlePHP operation mongoDB instance analysis, phpmongodb instance analysis This article describes the method of php operating mongoDB. Share it with everyone for your reference. The specific analysis is as follows: mongoDB database is a...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template