[MongoDB] mongodb與php php mongodb update php連接mongodb php mongodb未授權

WBOY
發布: 2016-07-29 08:52:58
原創
1043 人瀏覽過

windows上安裝mongodb的php擴充

下載位址https://s3.amazonaws.com/drivers.mongodb.org/php/index.html

找到對應的php版本的dll文件,下載php_mongo.dll,放到php安裝目錄下的ext目錄中,修改php.ini,新增一項extension=php_mongo.dll,沒有找到支援php7的dll

取得MongoClient對象,new出來

取得資料庫物件db,透過MongoClient物件的資料庫屬性,$MongoClient->資料庫名稱

取得集合collection,透過db物件的集合屬性,$db->集合名稱

建立集合,呼叫db物件的createCollection()方法,

呼叫collection物件的find()方法,查詢數據,$collection->find()

呼叫collection物件的update ()方法,更新數據,$collection->update($condition,$data);

呼叫collection物件的insert ()方法,插入數據,$collection->insert($data);

<?<span>php
</span><span>//</span><span> 连接到mongodb</span><span>$mongoClient</span> = <span>new</span><span> MongoClient();
</span><span>//</span><span> 选择一个数据库</span><span>$db</span> = <span>$mongoClient</span>-><span>test;

</span><span>//</span><span>获取集合</span><span>$collection</span>=<span>$db</span>-><span>users;

</span><span>//</span><span>更新文档</span><span>$condition</span>=<span>array</span><span>();
</span><span>$condition</span>["id"]=1<span>;
</span><span>$data</span>=<span>array</span><span>();
</span><span>$data</span>['name']="wangwu"<span>;
</span><span>$data</span>['age']="11"<span>;
</span><span>$collection</span>->update(<span>$condition</span>,<span>$data</span><span>);

</span><span>//</span><span>插入文档</span><span>$data</span>=<span>array</span><span>();
</span><span>$data</span>['id']=4<span>;
</span><span>$data</span>['name']="哈哈"<span>;
</span><span>$data</span>['age']="11"<span>;
</span><span>$collection</span>->insert(<span>$data</span><span>);

</span><span>//</span><span>删除文档</span><span>$condition</span>=<span>array</span><span>();
</span><span>$condition</span>['id']=2<span>;
</span><span>$collection</span>->remove(<span>$condition</span><span>);

</span><span>//</span><span>查询文档</span><span>$users</span>=<span>$collection</span>-><span>find();
</span><span>foreach</span> (<span>$users</span><span>as</span><span>$k</span> => <span>$v</span><span>) {
    </span><span>print_r</span>(<span>$v</span><span>);
}
</span>?>
登入後複製

以上就介紹了[MongoDB] mongodb與php,包含了mongodb,php方面的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!