當前位置:首頁 » 編程語言 » phpmongoor

phpmongoor

發布時間: 2022-04-05 01:27:08

A. mongodb 請問php中的這句mysql語法,在mongodb中如何寫。

查詢:
MySQL:
SELECT * FROM user
Mongo:
db.user.find()
MySQL:
SELECT * FROM user WHERE name = 'starlee'
Mongo:
db.user.find({『name' : 'starlee'})
插入:
MySQL:
INSERT INOT user (`name`, `age`) values ('starlee',25)
Mongo:
db.user.insert({『name' : 'starlee', 『age' : 25})
如果你想在MySQL里添加一個欄位,你必須:
ALTER TABLE user….
但在MongoDB里你只需要:
db.user.insert({『name' : 'starlee', 『age' : 25, 『email' : '[email protected]'})
刪除:
MySQL:
DELETE * FROM user
Mongo:
db.user.remove({})
MySQL:
DELETE FROM user WHERE age < 30
Mongo:
db.user.remove({『age' : {$lt : 30}})
$gt : > ; $gte : >= ; $lt : < ; $lte : <= ; $ne : !=
更新:
MySQL:
UPDATE user SET `age` = 36 WHERE `name` = 'starlee'
Mongo:
db.user.update({『name' : 'starlee'}, {$set : {『age' : 36}})
MySQL:
UPDATE user SET `age` = `age` + 3 WHERE `name` = 'starlee'
Mongo:
db.user.update({『name' : 'starlee'}, {$inc : {『age' : 3}})
MySQL:
SELECT COUNT(*) FROM user WHERE `name` = 'starlee'
Mongo:
db.user.find({『name' : 'starlee'}).count()
MySQL:
SELECT * FROM user limit 10,20
Mongo:
db.user.find().skip(10).limit(20)
MySQL:
SELECT * FROM user WHERE `age` IN (25, 35,45)
Mongo:
db.user.find({『age' : {$in : [25, 35, 45]}})
MySQL:
SELECT * FROM user ORDER BY age DESC
Mongo:
db.user.find().sort({『age' : -1})
MySQL:
SELECT DISTINCT(name) FROM user WHERE age > 20
Mongo:
db.user.distinct(『name', {『age': {$lt : 20}})
MySQL:
SELECT name, sum(marks) FROM user GROUP BY name
Mongo:
db.user.group({
key : {『name' : true},
cond: {『name' : 『foo'},
rece: function(obj,prev) { prev.msum += obj.marks; },
initial: {msum : 0}
});
MySQL:
SELECT name FROM user WHERE age < 20
Mongo:
db.user.find(『this.age < 20′, {name : 1})
發現很多人在搜MongoDB循環插入數據,下面把MongoDB循環插入數據的方法添加在下面:
for(var i=0;i<100;i++)db.test.insert({uid:i,uname:'nosqlfan'+i});
上面一次性插入一百條數據,大概結構如下:
{ 「_id」 : ObjectId(「4c876e519e86023a30dde6b8″), 「uid」 : 55, 「uname」 : 「nosqlfan55″ }
{ 「_id」 : ObjectId(「4c876e519e86023a30dde6b9″), 「uid」 : 56, 「uname」 : 「nosqlfan56″ }
{ 「_id」 : ObjectId(「4c876e519e86023a30dde6ba」), 「uid」 : 57, 「uname」 : 「nosqlfan57″ }
{ 「_id」 : ObjectId(「4c876e519e86023a30dde6bb」), 「uid」 : 58, 「uname」 : 「nosqlfan58″ }
{ 「_id」 : ObjectId(「4c876e519e86023a30dde6bc」), 「uid」 : 59, 「uname」 : 「nosqlfan59″ }
{ 「_id」 : ObjectId(「4c876e519e86023a30dde6bd」), 「uid」 : 60, 「uname」 : 「nosqlfan60″ }
簡易對照表
SQL Statement Mongo Query Language Statement
CREATE TABLE USERS (a Number, b Number) implicit; can be done explicitly
INSERT INTO USERS VALUES(1,1) db.users.insert({a:1,b:1})
SELECT a,b FROM users db.users.find({}, {a:1,b:1})
SELECT * FROM users db.users.find()
SELECT * FROM users WHERE age=33 db.users.find({age:33})
SELECT a,b FROM users WHERE age=33 db.users.find({age:33}, {a:1,b:1})
SELECT * FROM users WHERE age=33 ORDER BY name db.users.find({age:33}).sort({name:1})
SELECT * FROM users WHERE age>33 db.users.find({'age':{$gt:33}})})
SELECT * FROM users WHERE age<33 db.users.find({'age':{$lt:33}})})
SELECT * FROM users WHERE name LIKE "%Joe%" db.users.find({name:/Joe/})
SELECT * FROM users WHERE name LIKE "Joe%" db.users.find({name:/^Joe/})
SELECT * FROM users WHERE age>33 AND age<=40 db.users.find({'age':{$gt:33,$lte:40}})})
SELECT * FROM users ORDER BY name DESC db.users.find().sort({name:-1})
CREATE INDEX myindexname ON users(name) db.users.ensureIndex({name:1})
CREATE INDEX myindexname ON users(name,ts DESC) db.users.ensureIndex({name:1,ts:-1})
SELECT * FROM users WHERE a=1 and b='q' db.users.find({a:1,b:'q'})
SELECT * FROM users LIMIT 10 SKIP 20 db.users.find().limit(10).skip(20)
SELECT * FROM users WHERE a=1 or b=2 db.users.find( { $or : [ { a : 1 } , { b : 2 } ] } )
SELECT * FROM users LIMIT 1 db.users.findOne()
EXPLAIN SELECT * FROM users WHERE z=3 db.users.find({z:3}).explain()
SELECT DISTINCT last_name FROM users db.users.distinct('last_name')
SELECT COUNT(*y) FROM users db.users.count()
SELECT COUNT(*y) FROM users where AGE > 30 db.users.find({age: {'$gt': 30}}).count()
SELECT COUNT(AGE) from users db.users.find({age: {'$exists': true}}).count()
UPDATE users SET a=1 WHERE b='q' db.users.update({b:'q'}, {$set:{a:1}}, false, true)
UPDATE users SET a=a+2 WHERE b='q' db.users.update({b:'q'}, {$inc:{a:2}}, false, true)
DELETE FROM users WHERE z="abc" db.users.remove({z:'abc'});
###################################################
一、操作符
操作符相信大家肯定都知道了,就是等於、大於、小於、不等於、大於等於、小於等於,但是在mongodb里不能直接使用這些操作符。在mongodb里的操作符是這樣表示的:
(1) $gt > (大於)
(2) $lt< (小於)
(3) $gte>= (大於等於)
(4) $lt<= (小於等於)
(5) $ne!= (不等於)
(6) $inin (包含)
(7) $ninnot in (不包含)
(8) $existsexist (欄位是否存在)
(9) $inc對一個數字欄位field增加value
(10) $set就是相當於sql的set field = value
(11) $unset就是刪除欄位
(12) $push把value追加到field裡面去,field一定要是數組類型才行,如果field不存在,會新增一個數組類型加進去
(13) $pushAll同$push,只是一次可以追加多個值到一個數組欄位內
(14) $addToSet增加一個值到數組內,而且只有當這個值不在數組內才增加。
(15) $pop刪除最後一個值:{ $pop : { field : 1 } }刪除第一個值:{ $pop : { field : -1 } }注意,只能刪除一個值,也就是說只能用1或-1,而不能用2或-2來刪除兩條。mongodb 1.1及以後的版本才可以用
(16) $pull從數組field內刪除一個等於value值
(17) $pullAll同$pull,可以一次刪除數組內的多個值
(18) $ 操作符是他自己的意思,代表按條件找出的數組裡面某項他自己。這個比較坳口,就不說了。
二、CURD 增、改、讀、刪
增加

復制代碼代碼如下:

db.collection->insert({'name' => 'caleng', 'email' => 'admin#admin.com'});

是不是灰常簡單呀,對就是這么簡單,它沒有欄位的限制,你可以隨意起名,並插入數據

復制代碼代碼如下:

db.collection.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } ); 只更新了第一條大於1記錄
db.collection.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true ); 大於3的記錄 全更新了
db.collection.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false ); 大於4的記錄 只加進去了第一條
db.collection.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true ); 大於5的記錄 全加進去

查詢

復制代碼代碼如下:

db.collection.find(array('name' => 'ling'), array('email'=>'[email protected]'))
db.collection.findOne(array('name' => 'ling'), array('email''[email protected]'))

大家可以看到查詢我用了兩種不同的寫法,這是為什麼,其實這跟做菜是一樣的,放不同的調料,炒出的菜是不同的味道。下面給大家說一下,這兩種調料的不同作用。
findOne()只返回一個文檔對象,find()返回一個集合列表。
也就是說比如,我們只想查某一條特定數據的詳細信息的話,我們就可以用findOne();
如果想查詢某一組信息,比如說一個新聞列表的時候,我們就可以作用find();
那麼我想大家這時一定會想到我想對這一個列表排序呢,no problem mongodb會為您全心全意服務

復制代碼代碼如下:

db.collection.find().sort({age:1}); //按照age正序排列
db.collection.find().sort({age:-1}); //按照age倒序排列
db.collection.count(); //得到數據總數
db.collection.limit(1); //取數據的開始位置
db.collection.skip(10); //取數據的結束位置
//這樣我們就實現了一個取10條數據,並排序的操作。

刪除
刪除有兩個操作 remove()和drop()

復制代碼代碼如下:

db.collection.remove({"name",'jerry'}) //刪除特定數據
db.collection.drop() //刪除集合內的所有數據

distinct操作

復制代碼代碼如下:

db.user.distinct('name', {'age': {$lt : 20}})

2. 熟悉MongoDB的數據操作語句,類sql
資料庫操作語法
mongo --path
db.AddUser(username,password) 添加用戶
db.auth(usrename,password) 設置資料庫連接驗證
db.cloneDataBase(fromhost) 從目標伺服器克隆一個資料庫
db.commandHelp(name) returns the help for the command
db.Database(fromdb,todb,fromhost) 復制資料庫fromdb---源資料庫名稱,todb---目標資料庫名稱,fromhost---源資料庫伺服器地址
db.createCollection(name,{size:3333,capped:333,max:88888}) 創建一個數據集,相當於一個表
db.currentOp() 取消當前庫的當前操作
db.dropDataBase() 刪除當前資料庫
db.eval(func,args) run code server-side
db.getCollection(cname) 取得一個數據集合,同用法:db['cname'] or db.cname
db.getCollenctionNames() 取得所有數據集合的名稱列表
db.getLastError() 返回最後一個錯誤的提示消息
db.getLastErrorObj() 返回最後一個錯誤的對象
db.getMongo() 取得當前伺服器的連接對象get the server connection object
db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair
db.getName() 返回當操作資料庫的名稱
db.getPrevError() 返回上一個錯誤對象
db.getProfilingLevel() ?什麼等級
db.getReplicationInfo() ?什麼信息
db.getSisterDB(name) get the db at the same server as this onew
db.killOp() 停止(殺死)在當前庫的當前操作
db.printCollectionStats() 返回當前庫的數據集狀態
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus() 返回當前資料庫是否為共享資料庫
db.removeUser(username) 刪除用戶
db.repairDatabase() 修復當前資料庫
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj:1}
db.setProfilingLevel(level) 0=off,1=slow,2=all
db.shutdownServer() 關閉當前服務程序
db.version() 返回當前程序的版本信息
數據集(表)操作語法
db.linlin.find({id:10}) 返回linlin數據集ID=10的數據集
db.linlin.find({id:10}).count() 返回linlin數據集ID=10的數據總數
db.linlin.find({id:10}).limit(2) 返回linlin數據集ID=10的數據集從第二條開始的數據集
db.linlin.find({id:10}).skip(8) 返回linlin數據集ID=10的數據集從0到第八條的數據集
db.linlin.find({id:10}).limit(2).skip(8) 返回linlin數據集ID=1=的數據集從第二條到第八條的數據
db.linlin.find({id:10}).sort() 返回linlin數據集ID=10的排序數據集
db.linlin.findOne([query]) 返回符合條件的一條數據
db.linlin.getDB() 返回此數據集所屬的資料庫名稱
db.linlin.getIndexes() 返回些數據集的索引信息
db.linlin.group({key:...,initial:...,rece:...[,cond:...]})
db.linlin.mapRece(mayFunction,receFunction,<optional params>)
db.linlin.remove(query) 在數據集中刪除一條數據
db.linlin.renameCollection(newName) 重命名些數據集名稱
db.linlin.save(obj) 往數據集中插入一條數據
db.linlin.stats() 返回此數據集的狀態
db.linlin.storageSize() 返回此數據集的存儲大小
db.linlin.totalIndexSize() 返回此數據集的索引文件大小
db.linlin.totalSize() 返回些數據集的總大小
db.linlin.update(query,object[,upsert_bool]) 在此數據集中更新一條數據
db.linlin.validate() 驗證此數據集
db.linlin.getShardVersion() 返回數據集共享版本號

B. mongodb 樂觀鎖怎麼使用php

sql中並發控制採用的樂觀鎖就是在記錄中增加版本號或timestamp,那麼MongoDB中如何實現呢?
Mongodb不善於處理事務,但提供了findAndModify命令。該命令允許對文檔進行原子性更新,並在同一次調用中返回:
代碼如如:
db.collection_yown.findAndModify(
{
query:{"name":"yown"},update:{"version":2},new:true or false
}
)
默認情況下,findAndModify命令會返回更新前的文檔,要是返回修改後的文檔,就把new設置為false.
Mongodb同時也提供update命令,這兩者的區別如下:
update和findAndModify都可以用做更新操作;
區別
findAndModify是有返回值的,輸出中的value欄位即返回修改之前的文檔,使用 new:true選項返回修改後的文檔。 update是更新操作,是沒有返回值的。
findAndModify 強調操作的原子性(atomically),比如用來實現自增1的操作或者操作隊列。屬於 get-and-set 式的操作,一般來講,findAndModify 比update操作稍慢,因為需要等待資料庫的響應。
另外findAndModify ,其中modify可以是update,還可以是remove
{
findAndModify: <string>,
query: <document>,
sort: <document>,
remove: <boolean>,
update: <document>,
new: <boolean>,
fields: <document>,
upsert: <boolean>
}

C. php操作mongoDB資料庫查詢的時候怎樣寫「或」這樣的多個條件查詢代碼

據我所知,目前mongoDB沒有「或」這個東西

但我剛才在網上查了下
發現了下面的信息,你參考下吧

在mongodb中有$or 操作符的,官網中給出的例子如下:

Simple:

db.foo.find( { $or : [ { a : 1 } , { b : 2 } ] } )

With another field

db.foo.find( { name : "bob" , $or : [ { a : 1 } , { b : 2 } ] } )

The $or operator retrieves matches for each or clause indivially and eliminates plicates when returning results. A number of $or optimizations are planned for 1.8. See this thread for details.
$or cannot be nested.

D. php mongo條件有and和or時應該怎樣寫

mongo操作
find方法

db.collection_name.find();

查詢所有的結果:

select * from users;

db.users.find();

指定返回那些列(鍵):

select name, skills from users;

db.users.find({}, {'name' : 1, 'skills' : 1});

補充說明: 第一個{} 放where條件 第二個{} 指定那些列顯示和不顯示 (0表示不顯示 1表示顯示)

where條件:

1.簡單的等於:

select name, age, skills from users where name = 'hurry';

db.users.find({'name' : 'hurry'},{'name' : 1, 'age' : 1, 'skills' : 1});

2.使用and

select name, age, skills from users where name = 'hurry' and age = 18;

db.users.find({'name' : 'hurry', 'age' : 18},{'name' : 1, 'age' : 1, 'skills' : 1});

3.使用or

select name, age, skills from users where name = 'hurry' or age = 18;

db.users.find({ '$or' : [{'name' : 'hurry'}, {'age' : 18}] },{'name' : 1, 'age' : 1, 'skills' : 1});

4.<, <=, >, >= ($lt, $lte, $gt, $gte )

select * from users where age >= 20 and age <= 30;

db.users.find({'age' : {'$gte' : 20, '$lte' : 30}});

5.使用in, not in ($in, $nin)

select * from users where age in (10, 22, 26);

db.users.find({'age' : {'$in' : [10, 22, 26]}});

6.匹配null

select * from users where age is null;

db.users.find({'age' : null);

7.like (mongoDB 支持正則表達式)

select * from users where name like "%hurry%";

db.users.find({name:/hurry/});

select * from users where name like "hurry%";

db.users.find({name:/^hurry/});

8.使用distinct

select distinct (name) from users;

db.users.distinct('name');

9.使用count

select count(*) from users;

db.users.count();

10.數組查詢 (mongoDB自己特有的)

如果skills是 ['java','python']

db.users.find({'skills' : 'java'}); 該語句可以匹配成功

$all

db.users.find({'skills' : {'$all' : ['java','python']}}) skills中必須同時包含java 和 python

$size

db.users.find({'skills' : {'$size' : 2}}) 遺憾的是$size不能與$lt等組合使用

$slice

db.users.find({'skills' : {'$slice : [1,1]}})

兩個參數分別是偏移量和返回的數量

11.查詢內嵌文檔

12.強大的$where查詢

db.foo.find();
{ "_id" : ObjectId("4e17ce0ac39f1afe0ba78ce4"), "a" : 1, "b" : 3, "c" : 10 }
{ "_id" : ObjectId("4e17ce13c39f1afe0ba78ce5"), "a" : 1, "b" : 6, "c" : 6 }

如果要查詢 b = c 的文檔怎麼辦?

> db.foo.find({"$where":function(){

for(var current in this){
for(var other in this){
if(current != other && this[current] == this[other]){
return true;
}
}
}
return false;

}});

{ "_id" : ObjectId("4e17ce13c39f1afe0ba78ce5"), "a" : 1, "b" : 6, "c" : 6 }

E. mongo php 操作 怎樣更新一條數據

PHP操作MongoDB資料庫的簡單示例。
Mongodb的常用操作
參看手冊,php官方的http://us2.php.net/manual/en/mongo.manual.php
也可以參看mongodb官方的教程。
一,Mognodb資料庫連接
1)、默認格式

復制代碼代碼示例:
$m=newMongo();
//這里採用默認連接本機的27017埠,當然也可以連接遠程主機如192.168.0.4:27017,如果埠是27017,埠可以省略。
2)、標准連接
$m=newMongo(「mongodb://${username}:${password}@localhost」);
實例:
復制代碼代碼示例:
$m=newMongo(「mongodb://127.0.0.1:27017/admin:admin」);

資料庫的用戶名和密碼都是admin
資料庫操作:
1)、插入數據:

復制代碼代碼示例:
<?php
//這里採用默認連接本機的27017埠,當然你也可以連接遠程主機如192.168.0.4:27017
//如果埠是27017,埠可以省略
$m=newMongo("mongodb://127.0.0.1:27017/admin:admin");
//選擇comedy資料庫,如果以前沒該資料庫會自動創建,也可以用$m->selectDB("comedy");
$db=$m->comedy;
//選擇comedy裡面的collection集合,相當於RDBMS裡面的表,也可以使用
$collection=$db->collection;
$db->selectCollection("collection");
/*********添加一個元素**************/
$obj=array("title"=>"php1","author"=>"BillWatterson");
//將$obj添加到$collection集合中
$collection->insert($obj);
/*********添加另一個元素**************/
$obj=array("title"=>"huaibei","online"=>true);
$collection->insert($obj);
//$query=array("title"=>"huaibei");
$query=array("_id"=>$obj['_id']);
$cursor=$collection->find($query);
//遍歷所有集合中的文檔
foreach($cursoras$obj){
echo$obj["title"]." ";
echo$obj["_id"]." ";
}
//斷開MongoDB連接
$m->close();
2)、帶條件的查詢
查詢title為huaibei的欄位
1$query=array(」title」=>」huaibei」);
2$cursor=$collection->find($query);//在$collectio集合中查找滿足$query的文檔
常用的SQL轉化為mongodb的條件

復制代碼代碼示例:
mysql:id=123
mongo:array(『id』=>123)
mysql:namelink』%bar%』
mongo:array(『name』=>newMongoRegex(『/.*bar.*/i』))
mysql:whereid>10
mongo:array(『id』=>array(『$gt』=>10))
mysql:whereid>=10
mongo:array(『id』=>array(『$gte』=>10))
mysql:whereid<10
mongo:array(『id』=>array(『$lt』=>10))
mysql:whereid<=10
mongo:array(『id』=>array(『$lte』=>10))
mysql:whereid>1andid<10
mongo:array(『id』=>array(『$gt』=>1,』$lt』=>10))
mysql:whereid<>10
mongo:array(『id』=>array(『$ne』=>10))
mysql:whereidin(123)
mongo:array(『id』=>array(『$in』=>array(1,2,3)))
mysql:whereidnotin(123)
mongo:array(『id』=>array(『$nin』=>array(1,2,3)))
mysql:whereid=2orid=9
mongo:array(『id』=>array(『$or』=>array(array(『id』=>2),array(『id』=>9))))
mysql:orderbynameasc
mongo:array(『sort』=>array(『name』=>1))
mysql:orderbynamedesc
mongo:array(『sort』=>array(『name』=>-1))
mysql:limit0,2
mongo:array(『limit』=>array(『offset』=>0,』rows』=>2))
mysql:selectname,email
mongo:array(『name』,'email』)
mysql:selectcount(name)
mongo:array(『COUNT』)//注意:COUNT為大寫

更詳細的轉換參考http://us2.php.net/manual/en/mongo.sqltomongo.php
注意事項:
查詢時,每個Object插入時都會自動生成一個獨特的_id,它相當於RDBMS中的主鍵,用於查詢時非常方便(_id每一都不同,很像自動增加的id)
例如:

復制代碼代碼示例:
<?php
$param=array("name"=>"joe");
$collection->insert($param);
$joe=$collection->findOne(array("_id"=>$param['_id']));
print_R($joe);
$m->close();

返回結果:Array([_id]=>MongoIdObject([$id]=>4fd30e21870da83416000002)[name]=>joe)
更改欄位值:

復制代碼代碼示例:
<?php
$sign=array("title"=>'php1');
$param=array("title"=>'php1','author'=>'test');
$joe=$collection->update($sign,$param);
刪除一個資料庫:

復制代碼代碼示例:
$m->dropDB(「comedy」);
列出所有可用資料庫:

復制代碼代碼示例:
$m->listDBs();//無返回值
附,mongodb常用的資料庫方法
MongoDB中有用的函數:
創建一個MongoDB對象

復制代碼代碼示例:
<?php
$mo=newMongo();
$db=newMongoDB($mo,』dbname』);//通過創建方式獲得一個MongoDB對象
刪除當前DB

復制代碼代碼示例:
<?php
$db=$mo->dbname;
$db->drop();
獲得當前資料庫名

復制代碼代碼示例:
<?php
$db=$mo->dbname;
$db->_tostring();
選擇想要的collection:

復制代碼代碼示例:
A:
$mo=newMongo();
$coll=$mo->dbname->collname;//獲得一個collection對象
B:
$db=$mo->selectDB(』dbname』);
$coll=$db->collname;
C:
$db=$mo->dbname;
$coll=$db->collname;
D:
$db=$mo->dbname;
$coll=$db->selectCollectoin(』collname』);//獲得一個collection對象
插入數據(MongoCollection對象):
http://us.php.net/manual/en/mongocollection.insert.php
MongoCollection::insert(array$a,array$options)
array$a要插入的數組
array$options選項
safe是否返回操作結果信息
fsync是否直接插入到物理硬碟
例子:

復制代碼代碼示例:
$coll=$mo->db->foo;
$a=array(』a』=>』b』);
$options=array(』safe』=>true);
$rs=$coll->insert($a,$options);

$rs為一個array型的數組,包含操作信息
刪除資料庫中的記錄(MongoCollection對象):
http://us.php.net/manual/en/mongocollection.remove.php
MongoCollection::remove(array$criteria,array$options)
array$criteria條件
array$options選項
safe是否返回操作結果
fsync是否是直接影響到物理硬碟
justOne是否隻影響一條記錄
例子:

復制代碼代碼示例:
$coll=$mo->db->coll;
$c=array(』a』=>1,』s』=>array(』$lt』=>100));
$options=array(』safe』=>true);
$rs=$coll->remove($c,$options);

$rs為一個array型的數組,包含操作信息
更新資料庫中的記錄(MongoCollection對象):
http://us.php.net/manual/en/mongocollection.update.php
MongoCollection::update(array$criceria,array$newobj,array$options)
array$criteria條件
array$newobj要更新的內容
array$options選項
safe是否返回操作結果
fsync是否是直接影響到物理硬碟
upsert是否沒有匹配數據就添加一條新的
multiple是否影響所有符合條件的記錄,默認隻影響一條
例子:

復制代碼代碼示例:
$coll=$mo->db->coll;
$c=array(』a』=>1,』s』=>array(』$lt』=>100));
$newobj=array(』e』=>』f』,』x』=>』y』);
$options=array(』safe』=>true,』multiple』=>true);
$rs=$coll->remove($c,$newobj,$options);

$rs為一個array型的數組,包含操作信息
查詢collection獲得單條記錄(MongoCollection類):
http://us.php.net/manual/en/mongocollection.findone.php
arrayMongoCollection::findOne(array$query,array$fields)
array$query條件
array$fields要獲得的欄位
例子:

復制代碼代碼示例:
$coll=$mo->db->coll;
$query=array(』s』=>array(』$lt』=>100));
$fields=array(』a』=>true,』b』=>true);
$rs=$coll->findOne($query,$fields);

如果有結果就返回一個array,如果沒有結果就返回NULL
查詢collection獲得多條記錄(MongoCollection類):
http://us.php.net/manual/en/mongocollection.find.php
MongoCursorMongoCollection::find(array$query,array$fields)
array$query條件
array$fields要獲得的欄位
例子:

復制代碼代碼示例:
$coll=$mo->db->coll;
$query=array(』s』=>array(』$lt』=>100));
$fields=array(』a』=>true,』b』=>true);
$cursor=$coll->find($query,$fields);
//排序
$cursor->sort(array(『欄位』=>-1));(-1倒序,1正序)
//跳過部分記錄
$cursor->skip(100);跳過100行
//只顯示部分記錄
$cursor->limit(100);只顯示100行
返回一個游標記錄對象MongoCursor。
針對游標對象MongoCursor的操作(MongoCursor類):
http://us.php.net/manual/en/class.mongocursor.php
循環或結果記錄:

復制代碼代碼示例:
$cursor=$coll->find($query,$fields);
while($cursor->hasNext()){
$r=$cursor->getNext();
var_mp($r);
}
或者
$cursor=$coll->find($query,$fields);
foreache($cursoras$k=>$v){
var_mp($v);
}
或者
$cursor=$coll->find($query,$fields);
$array=iterator_to_array($cursor);

F. mongodb有沒有免費的類似ops manager 管理工具

RockMongo 是一個PHP5寫的MongoDB管理工具。

主要特徵:

使用寬松的New BSD License協議
速度快,安裝簡單
支持10種國家和地區語言
插件系統:允許任何人開發自己的插件
模板系統:可以定製自己的模板
系統
可以配置多個主機,每個主機可以有多個管理員
需要管理員密碼才能登入操作,確保資料庫的安全性
伺服器
伺服器信息 (WEB伺服器, PHP, PHP.ini相關指令 ...)
狀態
資料庫信息
資料庫
查詢,創建和刪除
執行命令和Javascript代碼
統計信息
用戶管理
Profile
數據轉移
導入導出
集合(相當於表)
強大的查詢工具
讀數據,寫數據,更改數據,復制數據,刪除數據
查詢、創建和刪除索引
清空數據
批量刪除和更改數據
統計信息
改名
導入導出

GitHub地址:https://github.com/iwind/rockmongo

phpMoAdmin

phpMoAdmin 是一個用 PHP 開發的在線 MongoDB 管理工具,可用於創建、刪除和修改資料庫和索引,提供視圖和數據搜索工具,提供資料庫啟動時間和內存的統計,支持 JSON 格式數據的導入導出。

Nothing to configure - place the moadmin.php file anywhere on your site and it just works!
Fast AJAX-driven XHTML 1.1 interface operates consistently in every browser!
Self-contained in a single 95kb file!
Works on any version of PHP5 with the MongoDB NoSQL database & Mongo PHP driver.
Enter into the single smart-search box:
Plain text
(type-casted) value

Text with * wildcards
Regular Expressions (regex)
JSON (with Mongo-operators enabled!)
Includes multiple design themes to choose from
Super flexible - option to query MongoDB using JSON or PHP-array syntax
Import/export data in JSON format
Insert only new records
Save / upsert (adds & overwrites)
Update only pre-existing records
Batch-Insert until a plicate is found
Export full collections
Export the results of any query
Import can:
Textareas can be resized by dragging/stretching the lower-right corner.
E_STRICT PHP code is formatted to the Zend Framework coding standards + fully-documented in the phpDocumentor DocBlock standard.
Instructional error messages - phpMoAdmin can be used as a PHP-Mongo connection debugging tool
Option to enable password-protection for one or more users; to
activate protection, just add the username-password(s) to the array at
the top of the file.

UMongo

UMongo是一個基於Java的GUI應用程序,可以瀏覽和管理MongoDB的集群。它是可用於Linux,Windows和Mac OSX。

connect to a single server, a replica set, or a MongoS instance
DB ops: create, drop, authenticate, command, eval, …
Collection ops: create, rename, drop, find, insert, save, …
Document ops: update, plicate, remove, …
Index ops: create, drop, …
Shard ops: enable sharding, add shard, shard collection, …
GUI Document builder
Import / Export data from database to local files in JSON, BSON, CSV format.
Support for query options and write concerns (getLastError)
Display of numerous stats (server status, db stats, replication info, etc)
Mongo tree refreshes to have a real time view of cluster (servers up/down, rability, etc)
All operations are executed in background to keep UI responsive
Background threads can repeat commands automatically
GUI is identical on all OS

Genghis

一個簡潔明了的GUI管理控制台,已經發布了Ruby和PHP版本。

Genghis是一個單文件,提供了非常方便自託管和基於Web的解決方案。

它可以安成一個 Ruby gem 或作為一個單獨的PHP腳本

Genghis能夠管理任意伺服器上的任意資料庫,實現集合和文檔的管理。

這個應用的界面是響應式,所以也適合於在移動瀏覽器上使用。

G. PHP使用pdo連接access資料庫並循環顯示數據操作示例

本文實例講述了PHP使用pdo連接access資料庫並循環顯示數據操作。分享給大家供大家參考,具體如下:
PDO連接與查詢:
try
{
$conn
=
new
PDO("odbc:driver={microsoft
access
driver
(*.mdb)};
dbq=".realpath("MyDatabase.mdb"))
or
die("鏈接錯誤!");
//echo
"鏈接成功!";
}
catch(PDOException
$e){
echo
$e->getMessage();
}
$sql
=
"select
*
from
users";
1.
foreach()方法
foreach
($conn->query($sql)
as
$row)
{
$row["UserID"];
$row["UserName"];
$row["UserPassword"];
}
2.
while()方法
$rs
=
$conn->query($sql);
$rs->setFetchMode(PDO::FETCH_NUM);
while($row=$rs->fetch()){
$row[0];
$row[1];
$row[2];
}
php使用PDO抽象層獲取查詢結果,主要有三種方式:
(1)PDO::query()查詢。
看下面這段php代碼:
<?php
//PDO::query()查詢
$res
=
$db->query('select
*
from
user');
$res->setFetchMode(PDO::FETCH_NUM);
//數字索引方式
while
($row
=
$res->fetch()){
print_r($row);
}
?>
(2)PDO->exec()處理sql
<?php
//PDO->exec()處理sql
$db->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
$res
=
$db->exec("insert
into
user(id,name)
values('','php點點通')");
echo
$res;
?>
(3)PDO::prepare()預處理執行查詢
<?php
//PDO::prepare()預處理執行查詢
$res
=
$db->prepare("select
*
from
user");
$res->execute();
while
($row
=
$res->fetchAll())
{
print_r($row);
}
?>
setAttribute()
方法是設置屬性,常用參數如下:
PDO::CASE_LOWER
--
強制列名是小寫
PDO::CASE_NATURAL
--
列名按照原始的方式
PDO::CASE_UPPER
--
強制列名為大寫
setFetchMode方法來設置獲取結果集的返回值的類型,常用參數如下:
PDO::FETCH_ASSOC
--
關聯數組形式
PDO::FETCH_NUM
--
數字索引數組形式
PDO::FETCH_BOTH
--
兩者數組形式都有,這是默認的
PDO::FETCH_OBJ
--
按照對象的形式,類似於以前的
mysql_fetch_object()
對上面總結如下:
查詢操作主要是PDO::query()、PDO::exec()、PDO::prepare()。
PDO->query()

處理一條SQL語句,並返回一個「PDOStatement」
PDO->exec()

處理一條SQL語句,並返回所影響的條目數
PDO::prepare()主要是預處理操作,需要通過$rs->execute()來執行預處理裡面的SQL語句
最後介紹兩個常用的函數:
(1)fetchColumn()獲取指定記錄里一個欄位結果,默認是第一個欄位!
<?php
$res
=
$db->query('select
*
from
user');
//獲取指定記錄里第二個欄位結果
$col
=
$res->fetchColumn(1);
echo
$col;
?>
(2)fetchAll(),從一個結果集中獲取數據,然後存放在關聯數組中
<?php
$res
=
$db->query('select
*
from
user');
$res_arr
=$res->fetchAll();
print_r($res_arr);
?>
更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP基於pdo操作資料庫技巧總結》、《php+Oracle資料庫程序設計技巧總結》、《PHP+MongoDB資料庫操作技巧大全》、《php面向對象程序設計入門教程》、《php字元串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
您可能感興趣的文章:PHP使用PDO連接ACCESS資料庫PHP資料庫鏈接類(PDO+Access)實例分享php中mysql連接方式PDO使用詳解關於php連接mssql:pdo
odbc
sql
serverPhp中用PDO查詢Mysql來避免SQL注入風險的方法php中在PDO中使用事務(Transaction)全新的PDO資料庫操作類php版(僅適用Mysql)php使用pdo連接並查詢sql資料庫的方法php使用pdo連接mssql
server資料庫實例PHP實現PDO的mysql資料庫操作類

熱點內容
安卓實時更新圖片的控制項叫什麼 發布:2025-07-29 19:58:10 瀏覽:270
python調用perl 發布:2025-07-29 19:49:13 瀏覽:780
angular路由緩存 發布:2025-07-29 19:47:32 瀏覽:534
安卓蘋果怎麼藍牙互傳視頻 發布:2025-07-29 19:36:30 瀏覽:422
7z軟體解壓縮 發布:2025-07-29 19:23:49 瀏覽:726
華碩筆記本怎麼設密碼 發布:2025-07-29 19:22:23 瀏覽:792
安卓錄音怎麼使用 發布:2025-07-29 19:22:23 瀏覽:23
資料庫封裝庫 發布:2025-07-29 19:22:21 瀏覽:413
千年腳本定製 發布:2025-07-29 19:19:56 瀏覽:941
pythonclass類 發布:2025-07-29 19:17:23 瀏覽:465