As mentioned above, for database connection operations, please refer to: Detailed description of mongodb user permission settings for database operations: see: mongodb database creation, switchover, and deletion, database Table insert operation 1, command line insert operation usetest; # Switch to test database switchedtodbtestdocument ({title:
As mentioned above, for database connection operations, please refer to: Detailed description of mongodb user permission settings for database operations: see: mongodb database creation, switchover, and deletion, insert operation 1 of the database table and use test in the command line; # Switch to the test database switched to db test document = ({"title ":
For more information about database connection, see:Detailed description of mongodb user permission settings
Database Operations: see:Create, switch, and delete mongodb Databases
The following describes how to insert a database table.
1. insert operation under the command line
> Use test; # Switch to the test database switched to db test> document = ({"title": "linux Command", "auther": "tank "}); # defines a variable {"title": "linux Command", "auther": "tank"}> db. test. insert (document); # insert variable> db. test. find (); # view inserted data {"_ id": ObjectId ("53c8fc1cf062ac30ee8b9d2d"), "title": "linux Command", "auther ": "tank"}> db. test. insert ({"title": "51yip", "auther": "tank"}); # insert data directly> db. test. find (); # view {"_ id": ObjectId ("53c8fc1cf062ac30ee8b9d2d"), "title": "linux Command", "auther ": "tank" }{ "_ id": ObjectId ("53c8f6fff062ac30ee8b9d2e"), "title": "51yip", "auther": "tank "}
2. Use php to extend insert data
SelectDB ("test"); // select the database to operate. If the database does not exist, $ collection = $ curDB-> selectCollection ("test") is automatically created "); // select a set (understood as a table). If it does not exist, it will automatically create // $ collection-> drop (); // clear the set testCollection $ count = $ collection-> count (); // view the data volume in the Set echo "there is [" in the set before insert [". $ count. "] data entries"; // The two data entries are inserted under the main command line. Echo "*********** mongodb php insert *************"; $ obj = array ("title" => "besieged city", "auther" => "Qian Zhongshu"); $ rel = $ collection-> insert ($ obj ); var_dump ($ rel); // print the inserted echo "added Object id :". $ obj ['_ id']. ""; $ obj = array ("title" => "Chao Fa Baidi City", "auther" => "Li Bai"); $ rel = $ collection-> insert ($ obj, array ('safe '=> true); // safe indicates whether the operation result is returned. The returned information is arrayprint_r ($ rel ); // The inserted result is the array echo "New Object id :". $ obj ['_ id']. ""; $ count = $ collec Tion-> count (); // view the data volume in the Set echo "after insert, the set contains [". $ count. "] data records";?> Running result: there are [2] pieces of data *********** mongodb php insert ************** bool (true) in the set before insert) id of the new object: 53c908c87f8b9ad7218b4568Array ([n] => 0 [connectionId] => 4 [err] => [OK] => 1) id of the new object: 53c908c87f8b9ad7218b4569after insert, the set contains [4] data records.
As mentioned above, for database connection operations, please refer to: Detailed description of mongodb user permission settings for database operations: see: mongodb database creation, switchover, and deletion, database Table insert operation 1, insert operation under command line> use test; # Switch to test database switched to db test> document = ({"title": "linux Command ", "auther": "tank"}); # defines a variable {"title": "linux Command", "auther": "tank"}> db. test. insert (document); [...]