MongoDB command Line and PHP insert data detailed _mongodb

Source: Internet
Author: User
Tags mongodb mongodb add

Previously said to the database connection operation, please refer to: MongoDB Add user and permissions set detailed
Operations on the database: Please refer to: MongoDB database operation-Create, switch, delete
Let's say that the insert operation of the database table
1, the insert operation under the command line

> Use test;    #切换到test数据库 
switched to DB Test 
 
> document= ({"title": "Linux Command", "Auther": "Tank"});   #定义了一个变量 
{"title": "Linux Command", "Auther": "Tank"} 
> Db.test.insert (document);     #插入变量 
> Db.test.find ();       #查看插入的数据 
{"_id": ObjectId ("53c8fc1cf062ac30ee8b9d2d"), "title": "Linux Command", "Auther": "Tank"} 
 
> Db.test. Insert ({"title": "51yip", "Auther": "Tank"});  #直接插入数据 
> Db.test.find ();       #查看 
{"_id": ObjectId ("53c8fc1cf062ac30ee8b9d2d"), "title": "Linux Command", "Auther": "Tank"} 
{"_id": ObjectId ( "53c8f6fff062ac30ee8b9d2e"), "title": "51yip", "Auther": "Tank"} 

2, using PHP to expand Insert data

<?php//$mongo = new MONGO ("mongodb://192.168.10.202:27017");          Link remote Database $mongo = new MONGO ();    Link remote Database $curDB = $mongo->selectdb ("test"); Select the database you want to manipulate, and if it does not exist, automatically create $collection = $curDB->selectcollection ("test");       Select a collection (interpreted as a table) and automatically create//$collection->drop () if it does not exist;     Empty the set testcollection $count = $collection->count (); View the amount of data in a collection there is [. $count.] In the collection before insert.  Data <Br> "; 
 
Here's the two data main command line inserted below. 
 
echo "<br>********** mongodb php insert into *************<br>"; 
$obj = Array ("title" => "Siege", "Auther" => "Qian Zhongshu"); 
$rel = $collection->insert ($obj);         Var_dump ($rel); The result of the print insert is a bool of echo "<Br> new Object ID:". $obj [' _id ']. " 
 
<Br> "; 
$obj = Array ("title" => "Chao Baidicheng", "Auther" => "Li Bai"); $rel = $collection->insert ($obj, Array (' safe ' =>true));         Safe indicates whether to return the operation result information, the return information is the array print_r ($rel); The result after insertion is the array echo "<Br> ID of the new object:". $obj [' _id ']. 
 
<Br> ";;     $count = $collection->count (); View in the collectionData volume echo "The collection has [". $count. "] 
 
Data <Br> "; 
 ?>

 
Run results: 
[2] data  
 
********** mongodb php insert in the collection before insert *************&nb Sp
BOOL (TRUE)  
New object's id:53c908c87f8b9ad7218b4568 
Array ([n] => 0 [ConnectionID] => 4 [err] =& Gt [OK] => 1  
New Object id:53c908c87f8b9ad7218b4569 
Insert the collection has [4] data  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.