This article mainly introduces the php Method for operating mongoDB. The example analyzes various common php skills for operating mongoDB, which is very useful. For more information, see
This article mainly introduces the php Method for operating mongoDB. The example analyzes various common php skills for operating mongoDB, which is very useful. For more information, see
This article describes how to operate mongoDB in php. Share it with you for your reference. The specific analysis is as follows:
MongoDB is a database stored in json format and is very suitable for various application development. Here we will introduce some mongoDB learning instances to you.
Mongodb needs to install the Mongo extension to integrate PHP. This is relatively simple. Let's talk about the MongoDB PHPAPI and usage.
Let's take a look at a simple example. The instance code is as follows:
The Code is as follows:
<? Php
$ M = new Mongo (); // use the default port 27017 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 the comedy database. If the database is not created before, it can be created automatically by using $ m-> selectDB ("comedy ");
$ Collection = $ db-> collection; // select the collection set 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 $ collection
$ Obj = array ("title" => "XKCD", "online" => true );
$ Collection-> insert ($ obj );
$ Cursor = $ collection-> find ();
Foreach ($ cursor as $ obj) {// traverse all documents in the Set
Echo $ obj ["title"]. "n ";
}
$ M-> close (); // disconnect MongoDB
The following describes some common functions. The Php code is as follows:
The Code is as follows:
$ Query = array ("I" => 71 );
$ Cursor = $ collection-> find ($ query); // search for documents that meet $ query in the $ collectio collection
While ($ cursor-> hasNext ()){
Var_dump ($ cursor-> getNext ());
}
$ Collection-> findOne (); // returns the first document in the $ collection.
$ Collection-> count (); // returns the number of documents in the $ collection.
$ Coll-> ensureIndex (array ("I" => 1); // Add an index in descending order for "This column" of I
$ Coll-> ensureIndex (array ("I" =>-1, "j" => 1 )); // Add an index to sort j in ascending order for I "This column"
During query, each Object is automatically inserted with a unique _ id, which is equivalent to the primary key in RDBMS and is very convenient for query. The Php code is as follows:
The Code is as follows:
<? Php
$ Person = array ("name" => "joe ");
$ People-> insert ($ person );
$ Joe = $ people-> findOne (array ("_ id" => $ person ['_ id']);
?>
Update: If you want to modify the author name in comments in the following document, the Php code is as follows:
The Code is as follows:
{
"_ Id": ObjectId ("4b06c282edb87a281e09dad9 "),
"Content": "this is a blog post .",
"Comments ":
[
{
"Author": "Mike ",
"Comment": "I think that blah ...",
},
{
"Author": "John ",
"Comment": "I disagree ."
}
]
}
To change an internal domain, we use $ set to ensure that other fields in the document are not removed, and the comment index also changes. The Php code is as follows:
The Code is as follows:
<? Php
$ Collection-> update ($ criteria, array ('$ set' => array ("comments.1" => array ("author" => "Jim ")))); // $ criteria is the element to be updated
?>
Delete A database. The Php code is as follows:
The Code is as follows:
$ M-> dropDB ("comedy ");
List all available databases. The Php code is as follows:
The Code is as follows:
$ M-> listDBs (); // No Return Value
Well, I will write so much. If you are interested, you can find other Mongo-php API usage on the Internet.
Command Line instance:
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', '123', '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 help you with php programming.