Shell 命令 和 Mysql 对比
Mongo | 明月三千里 | |
查询数据 | 等于:db.col.find({"delete_flag": "N"}).pretty() 小于:db.col.find({"count": {$lt: 10}}).pretty() 小于(lt: less than) 小于等于 (lte: less than or equal) 大于(gt: greater than) 大于等于 (gte: greater than or equal) | where delete_flag = 'N' where count < 10 |
更新数据 | db.col.update({'phone_number': { $gt : '18810167186' }}, {$set:{'full_name': 'Dake'}}, | update test set full_name = 'Dake' where phone_number = '18810167186' |
插入数据 | db.col.insert({ phone_number: '18810160000', full_name: 'Dake', delete_flag: 'N' }) | insert into test (phone_number, full_name, delete_flag) values ('18810160000', 'Dake', 'N') |
删除数据 | db.col.remove({'phone_number': '18810160000'}) | delete from test where phone_number = '18810160000' |
在 php 7.0 上的使用
1.查询
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017"); $filter = ['full_name' => ['$eq' => 'Dake']]; // where full_name = 'Dake' $options = []; $query = new MongoDB\Driver\Query($filter, $options); $cursor = $manager->executeQuery('test.col', $query); echo '<pre>'; foreach ($cursor as $document) { print_r($document); }
2.更新
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017"); $bulk = new MongoDB\Driver\BulkWrite; $bulk->update( ['user_name' => 'xiaoke01'], ['$set' => ['phone_number' => '28800010001', 'like_count' => '191']], ['multi' => false, 'upsert' => false] ); $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000); $result = $manager->executeBulkWrite('test.user', $bulk, $writeConcern);
3. 插入
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017"); $bulk = new MongoDB\Driver\BulkWrite; $bulk->insert(['user_name' => 'xiaoke01', 'phone_number' => '18800010001', 'delete_flag' => 'N', 'like_count' => 190]); $bulk->insert(['user_name' => 'xiaoke02', 'phone_number' => '18800010002', 'delete_flag' => 'N', 'like_count' => 390]); $bulk->insert(['user_name' => 'xiaoke03', 'phone_number' => '18800010003', 'delete_flag' => 'N', 'like_count' => 990]); $manager->executeBulkWrite('test.user', $bulk);
4.删除
$phone_number = $this->input->get('p'); $bulk = newMongoDB\Driver\BulkWrite; $bulk->delete(['phone_number' => $phone_number],['limit' => 1]);// limit 为 1 时,删除第一条匹配数据 $bulk->delete(['phone_number' => $phone_number],['limit' => 0]);// limit 为 0 时,删除所有匹配数据 $manager = newMongoDB\Driver\Manager("mongodb://localhost:27017"); $writeConcern = newMongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY,1000); $result = $manager->executeBulkWrite('test.user', $bulk, $writeConcern);