mongodb command line and insert data in php

  • 2020-05-07 20:37:26
  • OfStack

As mentioned earlier in the database connection operation, please refer to mongodb to add user and permission Settings for details
Database operations: please refer to mongodb database operations - create, switch, delete
So let's say 1, insert the database table
1. insert operation on the command line


> use test;    # Switch to the test The database  
switched to db test 
 
> document=({"title" : "linux The command ", "auther" : "tank" });   # Defines the 1 A variable  
{ "title" : "linux The command ", "auther" : "tank" } 
> db.test.insert(document);     # Insert the variable  
> db.test.find();       # View the inserted data  
{ "_id" : ObjectId("53c8fc1cf062ac30ee8b9d2d"), "title" : "linux The command ", "auther" : "tank" } 
 
> db.test.insert({"title" : "51yip", "auther" : "tank" });  # Direct insert data  
> db.test.find();       # To view  
{ "_id" : ObjectId("53c8fc1cf062ac30ee8b9d2d"), "title" : "linux The command ", "auther" : "tank" } 
{ "_id" : ObjectId("53c8f6fff062ac30ee8b9d2e"), "title" : "51yip", "auther" : "tank" } 

2. Extend insert data with php


<?php 
 
//$mongo = new Mongo("mongodb://192.168.10.202:27017"); // Link to remote database  
$mongo = new Mongo();          // Link to remote database  
$curDB = $mongo->selectDB("test");    // Select the database to operate on and automatically create it if it does not exist  
$collection = $curDB->selectCollection("test"); // The selected 1 Set of  table ), if it does not exist, it is created automatically  
//$collection->drop();       // Empty the collection  testCollection 
 
$count = $collection->count();     // View the amount of data in the collection  
echo "insert In the previous set [".$count."] The data <Br>";  // Here, 2 Data inserted under the main command line.  
 
echo "<br>********** mongodb php insert  insert  *************<br>"; 
 
$obj = array("title"=>" siege ","auther"=>" Qian zhongshu "); 
$rel = $collection->insert($obj); 
var_dump($rel);         // The result of the print insert is bool Type of  
echo "<Br> New object id : ".$obj['_id']."<Br>"; 
 
$obj = array("title"=>" Toward the white emperor city ","auther"=>" Li bai "); 
$rel = $collection->insert($obj,array('safe'=>true)); //safe  Indicates whether the operation result information is returned, and the returned information is  array 
print_r($rel);         // The inserted result is an array  
echo "<Br> New object id : ".$obj['_id']."<Br>";; 
 
$count = $collection->count();     // View the amount of data in the collection  
echo "insert In the later set [".$count."] The data <Br>"; 
 
?> 

 
Result:  
Before insert, there is a [2] bar of data  
 
* * * * * * * * * * mongodb php insert insert * * * * * * * * * * * * *  
bool(true)  
New object id: 53 c908c87f8b9ad7218b4568  
Array ( [n] = > 0 [connectionId] = > 4 [err] = > [ok] = > 1 )  
New object id: 53 c908c87f8b9ad7218b4569  
After insert, there are [4] pieces of data  


Related articles: