定制 rcastera/mongo 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rcastera/mongo

Composer 安装命令:

composer require rcastera/mongo

包简介

Mongo Abstraction Class

关键字:

README 文档

README

A simple class for working with MongoDB. Those new to Mongo, should go through the SQL to Mongo Mapping Chart

Setup

Add a composer.json file to your project:

{
  "require": {
      "rcastera/mongo": "v1.0.0"
  }
}

Then provided you have composer installed, you can run the following command:

$ composer.phar install

That will fetch the library and its dependencies inside your vendor folder. Then you can add the following to your .php files in order to use the library (if you don't already have one).

require 'vendor/autoload.php';

Then you need to use the relevant class, and instantiate the class. For example:

Getting Started

require 'vendor/autoload.php';

use rcastera\Database\Mongo\Monga;

$mongo = new Monga();

Examples

Find all documents from contacts collections.
<?php
    require 'vendor/autoload.php';
    use rcastera\Database\Mongo\Monga;

    $mongo = new Monga();
?>
<?php $cursor = $mongo->setDatabase('test')->setCollection('contacts')->getCollection()->find()->limit(10); ?>
<?php if ($cursor->hasNext()): ?>
<ul>
    <?php while ($cursor->hasNext()): ?>
    <?php $contact = $cursor->getNext(); ?>
    <li><?php echo $contact['name']; ?></li>
    <?php endwhile; ?>
</ul>
<?php else: ?>
<p>No contacts found.</p>
<?php endif; ?>
<?php unset($mongo); ?>
Find all documents and sort by name in ascending order.
<?php
    require 'vendor/autoload.php';
    use rcastera\Database\Mongo\Monga;

    $mongo = new Monga();
?>
<?php $cursor = $mongo->setDatabase('test')->setCollection('contacts')->getCollection()->find()->sort(array('name' => 1)); ?>
<?php if ($cursor->hasNext()): ?>
<ul>
    <?php while ($cursor->hasNext()): ?>
    <?php $contact = $cursor->getNext(); ?>
    <li><?php echo $contact['name']; ?></li>
    <?php endwhile; ?>
</ul>
<?php else: ?>
<p>No contacts found.</p>
<?php endif; ?>
<?php unset($mongo); ?>
Find all documents where name = 'Richard Castera'.
<?php
    require 'vendor/autoload.php';
    use rcastera\Database\Mongo\Monga;

    $mongo = new Monga();
?>
<?php $cursor = $mongo->setDatabase('test')->setCollection('contacts')->getCollection()->find(array('name' => 'Richard Castera')); ?>
<?php if ($cursor->hasNext()): ?>
<ul>
    <?php while ($cursor->hasNext()): ?>
    <?php $contact = $cursor->getNext(); ?>
    <li><?php echo $contact['name']; ?></li>
    <?php endwhile; ?>
</ul>
<?php else: ?>
<p>No contacts found.</p>
<?php endif; ?>
<?php unset($mongo); ?>
Find where name = 'Richard Castera' and update address.
<?php
    require 'vendor/autoload.php';
    use rcastera\Database\Mongo\Monga;

    $mongo = new Monga();

    $criteria = array(
        'name' => 'Richard Castera'
    );
    $updates = array(
        '$set' => array(
            'address' => '2 Smith Lane'
        )
    );
    $options = array(
        'upsert' => false
    );
?>
<?php $updated = $mongo->setDatabase('test')->setCollection('contacts')->getCollection()->update($criteria, $updates, $options); ?>
<?php if ($updated): ?>
<p>Contact updated.</p>
<?php else: ?>
<p>Contact not updated.</p>
<?php endif; ?>
<?php unset($mongo); ?>
Insert new document.
<?php
    require 'vendor/autoload.php';
    use rcastera\Database\Mongo\Monga;

    $mongo = new Monga();

    $document = array(
        'name' => 'Isabella Castera',
        'age' => '3',
        'address' => '3 Smith Lane'
    );
?>
<?php $inserted = $mongo->setDatabase('test')->setCollection('contacts')->getCollection()->insert($document); ?>
<?php if ($inserted): ?>
<p>Contact inserted.</p>
<?php else: ?>
<p>Contact not inserted.</p>
<?php endif; ?>
<?php unset($mongo); ?>
Delete a document.
<?php
    require 'vendor/autoload.php';
    use rcastera\Database\Mongo\Monga;

    $mongo = new Monga();

    $criteria = array(
        'name' => 'Elisabeth Castera'
    );

    $options = array(
        'justOne' => TRUE
    );
?>
<?php $deleted = $mongo->setDatabase('test')->setCollection('contacts')->getCollection()->delete($criteria, $options); ?>
<?php if ($deleted): ?>
<p>Contact deleted.</p>
<?php else: ?>
<p>Contact not deleted.</p>
<?php endif; ?>
<?php unset($mongo); ?>

Contributing

  1. Fork it.
  2. Create a branch (git checkout -b my_branch)
  3. Commit your changes (git commit -am "Added something")
  4. Push to the branch (git push origin my_branch)
  5. Create an Issue with a link to your branch
  6. Enjoy a refreshing Coke and wait

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-09-29

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固