mongodb 命令行下及php中insert數(shù)據(jù)詳解
前面說了到數(shù)據(jù)庫連接操作,請參考:mongodb 添加用戶及權(quán)限設(shè)置詳解
對數(shù)據(jù)庫的操作:請參考:mongodb 數(shù)據(jù)庫操作詳解--創(chuàng)建,切換,刪除
下面說一下,數(shù)據(jù)庫表的插入操作
1,命令行下的insert操作
> use test; #切換到test數(shù)據(jù)庫
switched to db test
> document=({"title" : "linux命令", "auther" : "tank" }); #定義了一個變量
{ "title" : "linux命令", "auther" : "tank" }
> db.test.insert(document); #插入變量
> db.test.find(); #查看插入的數(shù)據(jù)
{ "_id" : ObjectId("53c8fc1cf062ac30ee8b9d2d"), "title" : "linux命令", "auther" : "tank" }
> db.test.insert({"title" : "51yip", "auther" : "tank" }); #直接插入數(shù)據(jù)
> db.test.find(); #查看
{ "_id" : ObjectId("53c8fc1cf062ac30ee8b9d2d"), "title" : "linux命令", "auther" : "tank" }
{ "_id" : ObjectId("53c8f6fff062ac30ee8b9d2e"), "title" : "51yip", "auther" : "tank" }
2,利用php擴(kuò)展insert數(shù)據(jù)
<?php
//$mongo = new Mongo("mongodb://192.168.10.202:27017"); //鏈接遠(yuǎn)程數(shù)據(jù)庫
$mongo = new Mongo(); //鏈接遠(yuǎn)程數(shù)據(jù)庫
$curDB = $mongo->selectDB("test"); //選擇要操作的數(shù)據(jù)庫,如果不存在,則自動創(chuàng)建
$collection = $curDB->selectCollection("test"); //選中一個集合(理解為 table),如果不存在,則自動創(chuàng)建
//$collection->drop(); //清空集合 testCollection
$count = $collection->count(); //查看集合中的數(shù)據(jù)量
echo "insert前集合中有[".$count."]條數(shù)據(jù)<Br>"; //這里的二條數(shù)據(jù)主命令行下插入的。
echo "<br>********** mongodb php insert 插入 *************<br>";
$obj = array("title"=>"圍城","auther"=>"錢鐘書");
$rel = $collection->insert($obj);
var_dump($rel); //打印插入后的結(jié)果是bool型的
echo "<Br>新增對象的id:".$obj['_id']."<Br>";
$obj = array("title"=>"朝發(fā)白帝城","auther"=>"李白");
$rel = $collection->insert($obj,array('safe'=>true)); //safe 表示是否返回操作結(jié)果信息,返回的信息為 array
print_r($rel); //插入后的結(jié)果是數(shù)組
echo "<Br>新增對象的id:".$obj['_id']."<Br>";;
$count = $collection->count(); //查看集合中的數(shù)據(jù)量
echo "insert后集合中有[".$count."]條數(shù)據(jù)<Br>";
?>
運(yùn)行結(jié)果:
insert前集合中有[2]條數(shù)據(jù)
********** mongodb php insert 插入 *************
bool(true)
新增對象的id:53c908c87f8b9ad7218b4568
Array ( [n] => 0 [connectionId] => 4 [err] => [ok] => 1 )
新增對象的id:53c908c87f8b9ad7218b4569
insert后集合中有[4]條數(shù)據(jù)
相關(guān)文章
如何通過MongoDB?Atlas?實現(xiàn)語義搜索與?RAG(邁向AI的搜索機(jī)制)
MongoDBAtlas的語義搜索功能通過向量化存儲和檢索非結(jié)構(gòu)化數(shù)據(jù),結(jié)合RAG框架,實現(xiàn)了高效的知識檢索和增強(qiáng)型生成應(yīng)用,本文給大家介紹如何通過MongoDB?Atlas?實現(xiàn)語義搜索與?RAG(邁向AI的搜索機(jī)制),感興趣的朋友跟隨小編一起看看吧2024-11-11
在 Fedora 上安裝 MongoDB 服務(wù)器的方法教程
Mongo是一個高性能、開源、無模式的、面向文檔的數(shù)據(jù)庫,它是最受歡迎的 NoSQL 數(shù)據(jù)庫之一。這篇文章主要介紹了在 Fedora 上安裝 MongoDB 服務(wù)器的方法,需要的朋友可以參考下2020-03-03
MongoDB 監(jiān)控工具mongostat和mongotop的使用
這篇文章主要介紹了MongoDB 監(jiān)控工具mongostat和mongotop的使用方法,幫助大家更好的理解和學(xué)習(xí)使用MongoDB,感興趣的朋友可以了解下2021-03-03
MongoDB數(shù)據(jù)庫文檔操作方法(必看篇)
下面小編就為大家?guī)硪黄狹ongoDB數(shù)據(jù)庫文檔操作方法(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
mongodb eval 執(zhí)行服務(wù)器端腳本
在MongoDB的服務(wù)器端可以通過db.eval函數(shù)來執(zhí)行javascript腳本,如我們可以定義一個javascript函數(shù),然后通過db.eval在服務(wù)器端來運(yùn)行!我們前面其實也接觸過在服務(wù)器段運(yùn)行一個預(yù)定義的javascript腳本的情況,如在$where查詢,執(zhí)行mapreduce任務(wù)等。2015-05-05

