Detailed examples of PHP operations on MongoDB databases (add, delete, modify, and query) (6). mongodb Database

Source: Internet
Author: User

Detailed examples of PHP operations on MongoDB databases (add, delete, modify, and query) (6). mongodb Database
PHP operations on mongodb:

Modules are required for PHP to operate mongodb.
Official website can download: http://pecl.php.net/package/mongo download

Set mongodb to user-authorized Startup Mode

The php Manual does not have some user authorization methods for login:
Conn. php
<? Php
$ Conn = new Mongo ("mongodb: // user1: 123456 @ localhost: 27017/test"); // link to the mongodb test Database authorized by the user
$ Db = $ conn-> test;
?>

Find. php
<? Php
Include "conn. php ";
$ C1 = $ db-> c1; // operates the c1 set.

// Json cannot be used directly in php
// Db. c1.find ({name: "user1 "});
// {Name: "user1" }== array ("name" => "user1") in this form
// [1, 2] = array (1, 2 );
// {} = Array ()

$ Arr = array ();
$ Rst = $ c1-> find ($ arr );
Foreach ($ rst as $ val ){
Echo "<pre> ";
Print_r ($ val ['name']); // obtain "_ id" if the id is used"
}


Example 2: Query with specified values
$ Arr = array ("name" => "user1"); // query
$ Rst = $ c1-> find ($ arr );
Foreach ($ rst as $ val ){
Echo "<pre> ";
$ FS = $ val ['_ id'];
Print_r ($ val );
Echo "<a href = 'user. php? Fid = {$ fid} '> </a> "; // you will find that the fid is converted to a string when it is uploaded to user. php. How can this problem be solved?
// User. php query mongodb Data Based on _ id
<? Php
Include "conn. php ";
$ C1 = $ db-> c1;
$ Oid = new upload ID ($ _ GET ['fid']); use this to go through
Var_dump ($ oid); // or Object. If this parameter is not converted, it is of the string type.

$ Arr = array ("_ id" => "$ oid ");
$ Rst = $ c1-> find ($ arr );
Foreach ($ rst as $ val ){
Echo "<pre> ";
Print_r ($ val );
}
?>
}


Example 3: add
Include "conn. php ";
$ C1 = $ db-> c1;
// Db. c1.insert ({"name" => "user3", age: 30, "sex" => "nan "});
$ Arr = array ("name" => "user3", "age" => 30, "sex" => "nan ");
If ($ c1-> insert ($ arr ))
Echo 'successfully ';
Else
Echo 'failed ';




Example 4: Delete
Include "conn. php ";
$ C1 = $ db-> c1;
// Db. c1.remove ({"name" => "user2 "});
$ Arr = array ("name" => "user2 ");
If ($ c1-> remove ($ arr ))
Echo 'deleted successfully ';
Else
Echo 'deletion failed ';




Example 4: Modify
Include "conn. php ";
$ C1 = $ db-> c1;
// Db. c1.update ({"name" => "user2"}, {$ set: {age: 20, sex: "nan"}); add a field
$ Sarr = array ("name" => "user2 ");
$ Darr = array ('$ set' => array ('sex' => 'nan', 'age' => 24 ));
$ Opts = array ('upsert '=> 0, 'Multiple' => 1 );
If ($ c1-> update ($ sarr, $ darr, $ opts) // update in php can only pass three parameters
Echo 'changed successfully ';
Else
Echo 'change failed ';

// Close
$ Conn-> close ();
?>




How can I write multiple condition query codes such as "or" when I perform php operations on mongoDB database queries?

As far as I know, there is no "or" for mongoDB at present.

But I checked it online just now.
I found the following information. Please refer to it.

The $ or operator is available in mongodb. The example on the official website is as follows:

Simple:

Db. foo. find ({$ or: [{a: 1}, {B: 2}]})

With another field

Db. foo. find ({name: "bob", $ or: [{a: 1 },{ B: 2}]})

The $ or operator retrieves matches for each or clause individually and eliminates duplicates when returning results. A number of $ or optimizations are planned for 1.8. See this thread for details.
$ Or cannot be nested.

Use php to regularly query MongoDB once a day and store the obtained results to the MySQL database. How can I do this once a day for standby calls?

You can use cron to do this.
Write a php scrip containing the things to do
Call it with crontab
Note the file path

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.