Using PHP to implement the basic operation of MongoDB

Source: Internet
Author: User
Tags findone mongodb

Speaking of PHP even MongoDB, had to first introduce the official PHP manual, Web site in: http://us.php.net/manual/en/book.mongo.php

in the MONGO extension of PHP, 4 types of Interfaces (objects) are provided:

1, for MongoDB connection operation: Mongo

http://us.php.net/manual/en/class.mongo.php

2, for the operation of the database in MongoDB: MongoDB

http://us.php.net/manual/en/class.mongodb.php

3, for the operation of collection in MongoDB: mongocollection

http://us.php.net/manual/en/class.mongocollection.php

4, for query result set operations: Mongocursor

http://us.php.net/manual/en/class.mongocursor.php

establish a connection with MongoDB:

Directly instantiate the MONGO class + Create a connection:

$mo = new Mongo ();//Get a Mongo Connection object

An MONGO class was instantiated and a connection was established with the MongoDB of the default localhost:27017 port.

If you want to connect to another host, you can write this: $mongo = new MONGO ("mongodb://username:password@192.168.1.22:12345");

In another way, instantiate the MONGO class and manually establish the connection:

$mongo = new MONGO ("mongodb://username:password@192.168.1.22:12345", Array (' Connect ' =>false))//Initialize class

$mongo->connect ();//Create a connection

some useful methods in the MONGO class are:

Mongo::listdbs ()

http://us.php.net/manual/en/mongo.listdbs.php

Returns an array containing the library (DB) information on the current MONGO service.

$mo = new Mongo ();

$dbs = $mo->listdbs ();//get an array containing DB information

mongo::selectcollection ($db, $coll)

http://us.php.net/manual/en/mongo.selectcollection.php

Returns a collection object in a DB under the current connection.

$mo = new Mongo ();

$coll = $mo->selectcollection (' db ', ' mycoll ');//Get a Collection Object

Select the desired database (MONGO Class):

One way:

http://us.php.net/manual/en/mongo.get.php

$mongo = new MONGO ();

$db = $mongo->foo;//Get a MongoDB object

Another way:

http://us.php.net/manual/en/mongo.selectdb.php

$mongo = new MONGO ();

$db = $mongo->selectdb (' foo ');//Get a MongoDB object

useful functions in MongoDB:

Create a MongoDB object

http://us.php.net/manual/en/mongodb.construct.php

$mo = new Mongo ();

$db = new MongoDB ($mo, ' dbname ');//Get a MongoDB object by creating

Delete the current DB

http://us.php.net/manual/en/mongodb.drop.php

$db = $mo->dbname;

$db->drop ();

get the current database name

http://us.php.net/manual/en/mongodb.–tostring.php

$db = $mo->dbname;

$db->_tostring ();

Choose the collection you want:

A:

$mo = new Mongo ();

$coll = $mo->dbname->collname;//Get a Collection Object

B:

$db = $mo->selectdb (' dbname ');

$coll = $db->collname;

C:

$db = $mo->dbname;

$coll = $db->selectcollectoin (' collname ');//Get a Collection Object

Insert Data (Mongocollection object):

http://us.php.net/manual/en/mongocollection.insert.php

Mongocollection::insert (array $a, array $options)

Array $a arrays to insert

Array $options Options

Safe whether to return the action result information

Fsync whether to insert directly into the physical hard drive

Routines:

$coll = $mo->db->foo;

$a = Array (' A ' => ' B ');

$options = Array (' safe ' =>true);

$rs = $coll->insert ($a, $options);

$rs is an array of arrays that contains operation information

to delete a record in a database (Mongocollection object):

http://us.php.net/manual/en/mongocollection.remove.php

mongocollection::remove (array $criteria, array $options)

Array $criteria conditions

Array $options Options

Safe whether return operation result

Fsync whether it directly affects the physical hard drive

Whether Justone affects only one record

Routines:

$coll = $mo->db->coll;

$c = Array (' A ' =>1, ' s ' =>array (' $lt ' =>100));

$options = Array (' safe ' =>true);

$rs = $coll->remove ($c, $options);

$rs is an array of arrays that contains operation information

update records in the database (Mongocollection object):

http://us.php.net/manual/en/mongocollection.update.php

mongocollection::update (array $criceria, array $newobj, array $options)

Array $criteria conditions

Array $newobj What to update

Array $options Options

Safe whether return operation result

Fsync whether it directly affects the physical hard drive

Upsert If there is no matching data, add a new

Whether the multiple affects all records that meet the criteria, by default, affects only one

Routines:

$coll = $mo->db->coll;

$c = Array (' A ' =>1, ' s ' =>array (' $lt ' =>100));

$newobj = Array (' E ' => ' f ', ' x ' => ' y ');

$options = Array (' Safe ' =>true, ' multiple ' =>true);

$rs = $coll->remove ($c, $newobj, $options);

$rs is an array of arrays that contains operation information

Query collection get a single record (Mongocollection Class):

http://us.php.net/manual/en/mongocollection.findone.php

Array mongocollection::findone (array $query, array $fields)

Array $query conditions

Array $fields the field to get

Routines:

$coll = $mo->db->coll;

$query = Array (' s ' =>array (' $lt ' =>100));

$fields = Array (' A ' =>true, ' B ' =>true);

$rs = $coll->findone ($query, $fields);

Returns an array if there is a result, and returns null if there is no result

Query Collection get more than one record (Mongocollection Class):

http://us.php.net/manual/en/mongocollection.find.php

Mongocursor mongocollection::fInd (array $query, array $fields)

Array $query conditions

Array $fields the field to get

Routines:

$coll = $mo->db->coll;

$query = Array (' s ' =>array (' $lt ' =>100));

$fields = Array (' A ' =>true, ' B ' =>true);

$cursor = $coll->find ($query, $fields);

Returns a cursor Record object mongocursor.

actions for Cursor Object Mongocursor (Mongocursor Class):

http://us.php.net/manual/en/class.mongocursor.php

loop or result record:

$cursor = $coll->find ($query, $fields);

while ($cursor->hasnext ()) {

$r = $cursor->getnext ();

Var_dump ($R);

}

Or

$cursor = $coll->find ($query, $fields);

Foreache ($cursor as $k => $v) {

Var_dump ($v);

}

Or

$cursor = $coll->find ($query, $fields);

$array = Iterator_to_array ($cursor);

a lesson in blood:

http://us.php.net/manual/en/mongocursor.snapshot.php

See the ever-changing mongdb result set

After we do the find () operation and get the $cursor cursor, the cursor is still dynamic, that is, when I get the cursor to the completion of the corresponding record of my loop operation, by default, the result set increases automatically when the record of the eligible records increases. In other words, after I find (), by the time my cursor loop completes, if the qualifying records are inserted into the collection, then the records will be $cursor.

If you want the result set to be $cursor after you get it, you need to do this:

$cursor = $coll->find ($query, $fields);

$cursor->snapshot ();

Foreache ($cursor as $k => $v) {

Var_dump ($v);

}

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.