Home >php教程 >PHP开发 >Detailed explanation of insert data under mongodb command line and in php

Detailed explanation of insert data under mongodb command line and in php

高洛峰
高洛峰Original
2016-12-23 09:44:171626browse

I mentioned the database connection operation earlier, please refer to: mongodb detailed explanation of adding users and permission settings
Operations on the database: please refer to: mongodb detailed explanation of database operations--create, switch, delete
Let’s talk about the insertion operation of the database table
1. Insert operation under the command line

> use test;    #切换到test数据库 
switched to db test
  
> document=({"title" : "linux命令", "auther" : "tank" });   #定义了一个变量 
{ "title" : "linux命令", "auther" : "tank" } 
> db.test.insert(document);     #插入变量 
> db.test.find();       #查看插入的数据 
{ "_id" : ObjectId("53c8fc1cf062ac30ee8b9d2d"), "title" : "linux命令", "auther" : "tank" } 
  
> db.test.insert({"title" : "51yip", "auther" : "tank" });  #直接插入数据 
> db.test.find();       #查看 
{ "_id" : ObjectId("53c8fc1cf062ac30ee8b9d2d"), "title" : "linux命令", "auther" : "tank" } 
{ "_id" : ObjectId("53c8f6fff062ac30ee8b9d2e"), "title" : "51yip", "auther" : "tank" }

2. Use php to expand insert data

<?php 
  
//$mongo = new Mongo("mongodb://192.168.10.202:27017"); //链接远程数据库 
$mongo = new Mongo();          //链接远程数据库 
$curDB = $mongo->selectDB("test");    //选择要操作的数据库,如果不存在,则自动创建 
$collection = $curDB->selectCollection("test"); //选中一个集合(理解为 table),如果不存在,则自动创建 
//$collection->drop();       //清空集合 testCollection 
  
$count = $collection->count();     //查看集合中的数据量 
echo "insert前集合中有[".$count."]条数据<Br>";  //这里的二条数据主命令行下插入的。 
  
echo "<br>********** mongodb php insert 插入 *************<br>"; 
  
$obj = array("title"=>"围城","auther"=>"钱钟书"); 
$rel = $collection->insert($obj); 
var_dump($rel);         //打印插入后的结果是bool型的 
echo "<Br>新增对象的id:".$obj[&#39;_id&#39;]."<Br>"; 
  
$obj = array("title"=>"朝发白帝城","auther"=>"李白"); 
$rel = $collection->insert($obj,array(&#39;safe&#39;=>true)); //safe 表示是否返回操作结果信息,返回的信息为 array 
print_r($rel);         //插入后的结果是数组 
echo "<Br>新增对象的id:".$obj[&#39;_id&#39;]."<Br>";; 
  
$count = $collection->count();     //查看集合中的数据量 
echo "insert后集合中有[".$count."]条数据<Br>"; 
  
?>

Running results:
There are [2] pieces of data in the collection before insert

********** mongodb php insert Insert *************
bool(true)
The id of the new object: 53c908c87f8b9ad7218b4568
Array ( [n] => 0 [connectionId] => 4 [err] => [ok] => 1)
The id of the new object: 53c908c87f8b9ad7218b4569
There are [4] pieces of data in the collection after insert


For more detailed explanations of insert data under the mongodb command line and in php, please pay attention to PHP Chinese for related articles net!

Statement:
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