mongo数据库的简短操作代码
python版
#!/usr/bin/env python
-*- coding:utf-8 -*-
#设置编码,防止中文错误
import sys
reload(sys)
sys.setdefaultencoding('utf8')
from pymongo import MongoClient
class Mongo(object):
#构造函数
#host 数据库地址
#port 数据库端口
#user admin数据库用户
#password admin数据库用户的对应密码
def __init__(self, host, port,user,password):
self.host = host
self.port = port
self.conn = MongoClient(self.host, self.port)
admin_db = self.conn['admin']
admin_db.authenticate(user, password)
#返回连接操作句柄
def conn(self):
return self.conn
#返回数据库操作句柄
def db(self,db):
#连接db数据库,没有则自动创建
db = self.conn[db]
return db
#返回数据库的集合操作句柄
def set(self,target_db,target_set):
#使用set集合,没有则自动创建
db = self.conn[str(target_db)]
my_set = db[str(target_set)]
return my_set
php版
<?php
/**
*
*/
class _mongo_
{
//数据库连接实例
protected $instance;
//数据库
protected $db;
//集合(sql的表)
protected $db_set;
protected $writeConcern;
//条件
protected $where=[];
//排序
protected $sort = [];
protected $limit = 0;
protected $skip = 0;
protected $options = [];
protected $field = [];
public function __construct($db,$host='127.0.0.1',$port=27017,$user='myadmin',$password='1234@Hao')
{
if(!$db){
return false;
}
// $this->instance = new MongoDB\Driver\Manager("mongodb://$host:$port");
$this->instance = new \MongoDB\Driver\Manager("mongodb://$host:$port",['username' => $user,'password' => $password]);
$this->db = $db;
$this->writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$this->options['projection'] = [];
// $this->instance->authenticate($user,$password);
}
public function where($where){
if($where){
$this->where = $where;
}
return $this;
}
protected function unset(){
$this->options = [];
$this->where = [];
$this->sort = [];
$this->field = [];
$this->limit = 0;
$this->skip = 0;
}
public function find(){
if(!$this->db_set){
return false;
}
$query = new \MongoDB\Driver\Query($this->where, $this->options);
$cursor = $this->instance->executeQuery($this->db.'.'.$this->db_set, $query);
$result = [];
foreach ($cursor as $document) {
$tmp = [];
foreach ($document as $k => $v) {
$tmp[$k] = $v;
}
$result[] = $tmp;
}
$this->unset();
$result = json_encode($result[0]);
$result = json_decode($result,true);
return $result;
}
public function select(){
if(!$this->db_set){
return false;
}
$query = new \MongoDB\Driver\Query($this->where, $this->options);
$cursor = $this->instance->executeQuery($this->db.'.'.$this->db_set, $query);
$result = [];
foreach ($cursor as $document) {
$tmp = [];
foreach ($document as $k => $v) {
$tmp[$k] = $v;
}
$result[] = $tmp;
}
$this->unset();
$result = json_encode($result);
$result = json_decode($result,true);
return $result;
}
public function delete(){
if(!$this->where || !$this->db_set){
return false;
}
$bulk = new \MongoDB\Driver\BulkWrite;
$bulk->delete($this->where, ['limit' => $this->limit]); // limit 为 1 时,删除第一条匹配数据 limit 为 0 时,删除所有匹配数据
$result = $this->instance->executeBulkWrite($this->db.'.'.$this->db_set, $bulk, $this->writeConcern);
$this->unset();
return $result;
}
public function add($data){
$bulk = new MongoDB\Driver\BulkWrite;
$document = [];
$document['_id'] = new \MongoDB\BSON\ObjectID;
foreach ($data as $k => $v) {
$document[$k] = $v;
}
$_id= $bulk->insert($document);
$result = $this->instance->executeBulkWrite($this->db.'.'.$this->db_set, $bulk, $this->writeConcern);
unset($bulk);
$this->unset();
return ['id' => $document['_id'],'result' => $result];
}
/*
$multi 修改多条
$upsert 它为true的时候,update方法会首先查找与$where匹配的记录,再用$data更新之,如果找不到与$where匹配的的记录,就插入一条
*/
public function save($data,$multi=false,$upsert=false){
$bulk = new \MongoDB\Driver\BulkWrite;
$bulk->update(
$this->where,
$data,
['multi' => $multi, 'upsert' => $upsert]
);
$result = $this->instance->executeBulkWrite($this->db.'.'.$this->db_set, $bulk, $this->writeConcern);
$this->unset();
return $result;
}
public function page($limit){
$this->limit = $limit;
if(count(explode(',', $limit)) == 2){
$limit = explode(',', $limit);
$limit[0] = intval($limit[0]);
$limit[1] = intval($limit[1]);
$this->options['limit'] = $limit[1];
$this->options['skip'] = $limit[0]*$limit[1];
}
// var_dump($this->options);die;
return $this;
}
public function field($field){
if(count(explode(',', $field)) > 0){
$fields = explode(',', $field);
foreach ($fields as $k => $field) {
$this->options['projection'][$field] = 1;
}
}
// var_dump($this->options);die;
return $this;
}
public function no_field($field){
if(count(explode(',', $field)) > 0){
$fields = explode(',', $field);
foreach ($fields as $k => $field) {
$this->options['projection'][$field] = 0;
}
}
// var_dump($this->options);die;
return $this;
}
/*
排序 1为升序 -1为降序
*/
public function order($order){
if($order){
$orders = explode(',', $order);
foreach ($orders as $k => $order) {
$order = explode(' ', $order);
$this->options['sort'] = [];
$this->options['sort'][$order[0]] = intval($order[1]);
}
}else{
$this->sort = [];
}
return $this;
}
public function count(){
$cmd = ['count'=>$this->db_set,'query'=>$this->where];
$command = new \MongoDB\Driver\Command($cmd);
$result = $this->instance->executeCommand($this->db,$command);
$response= current($result->toArray());
if($response->ok==1){
return$response->n;
}
return 0;
}
public function set($set){
if(!$set){
return false;
}
$this->db_set = $set;
return $this;
}
}