Public Library-mongodb-Notes

Source: Internet
Author: User
Tags connect mongo mongoclient

1, http://cn2.php.net/manual/en/class.mongogridfs.php

classFilecontrollerextendssycontroller{/** @author YJ * @description to use the following two functions, you must first initialize * date:14-9-3*/     Public functioninit () {Header("Content-type:text/html;charset=utf-8"); //Connect MONGO and Initialize GFS//database naming file        $conn=New\mongoclient (); $db=$conn-picdb; //Get Gridfs Object        $prefix= ' pic '; $collection=$db->getgridfs ($prefix); return $collection; }    /** @author YJ * @description will pass all binaries on to MongoDB and return the file index ID value * Dedicated to form submission resources * DATE:14-9-2 * time:11:21 */     Public functionUpload$filename,$collection)    {        $id=$collection->storeupload ($filename);//gets the _id string in the ID object of the previous step        $id=Strval($id); return $id;    }    /** @author YJ * @description Delete the corresponding resource based on the ID value * date:14-9-3*/     Public functionDelete$id,$collection)    {//Convert ID string to ID object        $collection->delete (New\mongoid ($id)); }    /** * @author: YJ * Use resource ID and resource type to retrieve and display resources from MongoDB*/     Public functionget () {Header("Content-type:text/html;charset=utf-8"); $conn=New\mongoclient (); $db=$conn-picdb;//Get Gridfs Object        $prefix= ' pic '; $collection=$db->getgridfs ($prefix); //get ID and type        $id= I (' get.id '); $type= I (' Get.type '); //Return Data        $object=$collection->findone (Array(' _id ' =New\mongoid ($id))); Header(' Content-type: '.$type); Echo $object-getBytes (); }

Note that the ' _id ' stored in MongoDB is an object ID, also known as an objects ID. And the id we put into MySQL is a string ID. The conversion method is as follows:

$id Strval ($id);  // gets the string ID in the object ID
new \mongoid ($id)  // convert string ID to MONGO object ID

2. If you want to use MongoDB in other controllers

//initializing the MongoDB database$collection= R (' File/init ');//use a form to upload a file, $key is the name of the input label in the form$id= R (' File/upload ',Array($key,$collection));//according to the string ID $avatar _id to delete MONGO inside the corresponding. files. chunks file$collection->delete (New\mongoid ($avatar _id));//gets the resource directly from the MONGO based on the string ID and displays it according to the type$avatar _id} "/>

Public Library-mongodb-notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.