Pcel installed MongoDB solution for two problems

Source: Internet
Author: User
Tags pcel

Recently work needs, to use MongoDB, this is the use of pecl installed, with the standard MONGO use or there is a difference, here does not tell the difference, only two more typical questions to how to deal with, specific documents you can directly refer to the official PHP document: http://php.net/ manual/zh/book.mongodb.php

Question 1:mongodb How to use _id to query records:

The ID here is the ID that MONGO automatically generates when we insert a record.

(1) from the record to get the ID

functionGet_oid ($query _result=Array()) {//The $query _result here is a record of the queryif(Empty($query _result)){            return false; }Else{            $id _obj=$query _result[' _id ']; $id _array=Get_object_vars($id _obj); return $id _array[' OID ']; }}

(2) query related records from _id

$id New Mongodb\bson\objectid ($origin _id);  $origin _id $query array $id for the ID in the record); $query the query criteria for the normal query statement, using this condition, you can query the record of _ID as the specified ID.

Question 2: How to delete a collection

functionDrop_collection ($db,$collection){        $manager=NewMongodb\driver\manager (' mongodb://').$your _hosts.‘ /‘,$your _options); You need to change it to your host option.Try{            $server=$manager->selectserver (NewMongodb\driver\readpreference (mongodb\driver\readpreference::rp_primary)); $result=$server->executecommand ($db,NewMongodb\driver\command (["drop" + =$collection])); This sentence is the key to implementation, note the format        }Catch(Exception $E){           return $E; }        return $result;}

Pcel installation of two problems with MongoDB solution

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.