现在的位置: 首页mongodb>正文
mongodb增删查改
2013年07月18日 mongodb 暂无评论 ⁄ 被围观 10,771 view+
SQL Statement                                       Mongo Query Language Statement
INSERT INTO USERS VALUES(1,1)	                    $db->users->insert(array("a" => 1, "b" => 1));
SELECT a,b FROM users	                            $db->users->find(array(), array("a" => 1, "b" => 1));
SELECT * FROM users WHERE age=33	                $db->users->find(array("age" => 33));
SELECT a,b FROM users WHERE age=33	                $db->users->find(array("age" => 33), array("a" => 1, "b" => 1));
SELECT a,b FROM users WHERE age=33 ORDER BY name	$db->users->find(array("age" => 33), array("a" => 1, "b" => 1))->sort(array("name" => 1));
SELECT * FROM users WHERE age>33                    $db->users->find(array("age" => array('$gt' => 33)));
SELECT * FROM users WHERE age<33                    $db->users->find(array("age" => array('$lt' => 33)));
SELECT * FROM users WHERE name LIKE "%Joe%"	        $db->users->find(array("name" => new MongoRegex("/Joe/")));
SELECT * FROM users WHERE name LIKE "Joe%"	        $db->users->find(array("name" => new MongoRegex("/^Joe/")));
SELECT * FROM users WHERE age>33 AND age<=40        $db->users->find(array("age" => array('$gt' => 33, '$lte' => 40)));
SELECT * FROM users ORDER BY name DESC	            $db->users->find()->sort(array("name" => -1));
CREATE INDEX myindexname ON users(name)	            $db->users->ensureIndex(array("name" => 1));
CREATE INDEX myindexname ON users(name,ts DESC)	    $db->users->ensureIndex(array("name" => 1, "ts" => -1));
SELECT * FROM users WHERE a=1 and b='q'	            $db->users->find(array("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(array('$or' => array(array("a" => 1), array("b" => 2))));
SELECT * FROM users LIMIT 1	                        $db->users->find()->limit(1);
EXPLAIN SELECT * FROM users WHERE z=3	            $db->users->find(array("z" => 3))->explain()
SELECT DISTINCT last_name FROM users	            $db->command(array("distinct" => "users", "key" => "last_name"));
SELECT COUNT(*y) FROM users	                        $db->users->count();
SELECT COUNT(*y) FROM users where AGE > 30          $db->users->find(array("age" => array('$gt' => 30)))->count();
SELECT COUNT(AGE) from users	                    $db->users->find(array("age" => array('$exists' => true)))->count();
UPDATE users SET a=1 WHERE b='q'	                $db->users->update(array("b" => "q"), array('$set' => array("a" => 1)));
UPDATE users SET a=a+2 WHERE b='q'	                $db->users->update(array("b" => "q"), array('$inc' => array("a" => 2)));
DELETE FROM users WHERE z="abc"	                    $db->users->remove(array("z" => "abc"));

2. mongodb查询操作

Operator      Meaning                        Example                                             SQL Equivalent
$gt           Greater Than                   “score”:{"$gt”:0}                                    >
$lt           Less Than                      “score”:{"$lt”:0}                                    <
$gte          Greater Than or Equal          “score”:{"$gte”:0}                                   >=
$lte          Less Than or Equal             “score”:{"$lte”:0}                                   ?
$all          Array Must Contain All         “skills”:{"$all”:["mongodb”,"python"]}               N/A
$exists       Property Must Exist            “email”:{"$exists”:True}                             N/A
$mod          Modulo X Equals Y              “seconds”:{"$mod”:[60,0]}                            MOD()
$ne           Not Equals                     “seconds”:{"$ne”:60}                                 !=
$in           In                             “skills”:{"$in”:["c”,"c++"]}                          IN
$nin          Not In                         “skills”:{"$nin”:["php”,"ruby”,"perl"]}               NOT IN
$nor          Nor                            “$nor”:[{"language”:"english"},{"country”:"usa"}]     N/A
$or           Or                             “$or”:[{"language”:"english"},{"country”:"usa"}]      OR
$size         Array Must Be Of Size          “skills”:{"$size”:3}                                  N/A

3. mongodb更新修改

Modifier     Meaning                                          Example
$inc         Atomic Increment                                 “$inc”:{"score”:1}
$set         Set Property Value                               “$set”:{"username”:"niall"}
$unset       Unset (delete) Property                          “$unset”:{"username”:1}
$push        Atomic Array Append (atom)                       “$push”:{"emails”:"foo@example.com"}
$pushAll     Atomic Array Append (list)                       “$pushall”:{"emails”:["foo@example.com”,"foo2@example.com"]}
$addToSet    Atomic Append-If-Not-Present                     “$addToSet”:{"emails”:"foo@example.com"}
$pop         Atomic Array Tail Remove                         “$pop”:{"emails”:1}
$pull        Atomic Conditional Array Item Removal            “$pull”:{"emails”:"foo@example.com"}
$pullAll     Atomic Array Multi Item Removal                  “$pullAll”:{"emails”:["foo@example.com”, “foo2@example.com"]}
$rename      Atomic Property Rename                           “$rename”:{"emails”:"old_emails"}

4. 批量插入 将多个文档插入到一个集合中,使用批量插入来加快插入速度。在shell环境下,可以使用batchInsert函数。如:

>db.jiunile.batchInsert([{"_id":0},{"_id":1},{"_id":2},{"_id":3}])1

注意:

a. 批量插入只能针对单个集合。

b. 插入的大小不能超过48MB。

5. php连接mongodb

a. 对于启用验证
1
$connection = new MongoClient("mongodb://${username}:${password}@localhost");
$connection = new MongoClient("mongodb://localhost", array("username" => $username, "password" => $password));

b. 复制集

$connection = new MongoClient("mongodb://rs1.example.com:27017,rs2.example.com:27017/?replicaSet=myReplSetName"));
$connection = new MongoClient("mongodb://rs1.example.com:27017", array("replicaSet" => "myReplSetName"));
$connection = new MongoClient("mongodb://rs1.example.com:27017,rs2.example.com:27017", array("replicaSet" => "myReplSetName"));

c. 分片

$connection = new MongoClient("mongodb://mongos1.example.com:27017");
$connection = new MongoClient("mongodb://mongos1.example.com:27017,mongos2.example.com:27017"));

6. 选择数据库

$db = $connection->selectDB('dbname');
$db = $connection->dbname;

7. 创建或选择已存在的集合

$collection = $db->col_name;
$col_name = $connection->dbname->col_name;
1

8. 创建文档
1
$col_contant = array('website' => 'blog.jiunile.com', 'QQqun' => 644100988, 'email' => 'xupeng.js@gmail.com');
$col_name->insert($col_contant); //异步插入
$col_name->insert($col_contant, array('safe' => true)); //同步插入

9. 查询

$id = new MongoId('objectid_No.');
$row = $col_name->findone( array( '_id' => $id ) );
$row = $col_name->findone( array('website' => 'blog.jiunile.com'));
$e-mail = $db->col_name->findone( array('website' => 'blog.jiunile.com'),array('email'));

10. 更新文档

$col_name->update( array('_id' => new MongoId('objectid_No.')), array('$set' => array('email' => 'xupeng.js@gmail.com')));

11. 删除文档

$criteria = array('_id' => new MongoId('objectid_No.'));
$col_name->remove($criteria, array("justOne" => true));

来源:http://www.ttlsa.com/html/1636.html

给我留言

留言无头像?


×
腾讯微博