PHP MongoDB manager various poses for data checking

Source: Internet
Author: User
Tags php mongodb

I. Connect MONGODB database

The following actions, by default, are based on the above action (successful connection)

Second, the various posture of the check

1) query by primary key _id, note: _id is not directly represented by a string, but with the new \mongodb\bson\objectid object

//Mysql:select * from Appdownloadrecord WHERE _id= ' 5a2b9aaf96b4a97efbe31a91 ';$id=New\mongodb\bson\objectid ("5a2b9aaf96b4a97efbe31a91");$filter= [' _id ' + =$id];$options= [];$query=New\mongodb\driver\query ($filter,$options);$rows=$mongo->executequery (' Promotion-prod.appdownloadrecord ',$query),ToArray ();Echo' <pre> ';Print_r($rows);Echo' </pre> ';Exit;

2) Query the specified field (where _id is returned by default, and if you do not want to return it, set _id=>0)

//mysql:select ' source ', ' IP ' from Appdownloadrecord WHERE _id= ' 5a2b9aaf96b4a97efbe31a91 ';$id=New\mongodb\bson\objectid ("5a2b9aaf96b4a97efbe31a91");$filter= [' _id ' + =$id];$options= [    ' Projection ' = [' _id ' =>0, ' source ' =>1, ' IP ' =>1]];$query=New\mongodb\driver\query ($filter,$options);$rows=$mongo->executequery (' Promotion-prod.appdownloadrecord ',$query),ToArray ();Echo' <pre> ';Print_r($rows);Echo' </pre> ';Exit;

3) General condition query

//Mysql:select * from Appdownloadrecord WHERE source= ' bdsem1 ' LIMIT;$filter= [' Source ' = ' bdsem1 '];$options= [' Limit ' =>10];$query=New\mongodb\driver\query ($filter,$options);$rows=$mongo->executequery (' Promotion-prod.appdownloadrecord ',$query),ToArray ();Echo' <pre> ';Print_r($rows);Echo' </pre> ';Exit;

4) range query (between)

//Mysql:select * from Appdownloadrecord WHERE (source= ' bdsem1 ') and (Createtime bewteen ' 2017-12-21 00:00:00 ' and ' 201 7-12-28 00:00:00 ') LIMIT 10;//mysql:select * from Appdownloadrecord WHERE (source= ' bdsem1 ') and (createtime>= ' 2017-12 -21 00:00:00 ' and createtime< ' 2017-12-28 00:00:00 ') LIMIT ten;$startTime=New\mongodb\bson\utcdatetime (Strtotime(' 2017-12-21 00:00:00 ') *1000);$endTime=New\mongodb\bson\utcdatetime (Strtotime(' 2017-12-28 00:00:00 ') *1000);$filter= [    ' Source ' = ' bdsem1 ', ' createtime ' = [        ' $gte ' =$startTime, ' $lt ' =$endTime,    ],];$options= [' Limit ' =>10];$query=New\mongodb\driver\query ($filter,$options);$rows=$mongo->executequery (' Promotion-prod.appdownloadrecord ',$query),ToArray ();Echo' <pre> ';Print_r($rows);Echo' </pre> ';Exit;

5) in Query

//Mysql:select * from Appdownloadrecord WHERE (source in (' Bdsem1 ', ' besem2 ', ' besem3 ')) LIMIT ten;$filter= [    ' Source ' = [        ' $in ' = [' bdsem1 ', ' bdsem2 ', ' bdsem3 ']    ],];$options= [' Limit ' =>10];$query=New\mongodb\driver\query ($filter,$options);$rows=$mongo->executequery (' Promotion-prod.appdownloadrecord ',$query),ToArray ();Echo' <pre> ';Print_r($rows);Echo' </pre> ';Exit;

6) or operation

//Mysql:select * from Appdownloadrecord WHERE source= ' bdsem1 ' and (sys= ' iOS ' OR sys= ' other ') LIMIT ten;$filter= [    ' Source ' = ' bdsem1 ', ' $or ' + = [        [' sys ' = ' iOS '],        [' sys ' = ' other ']    ]];$options= [' Limit ' =>10];$query=New\mongodb\driver\query ($filter,$options);$rows=$mongo->executequery (' Promotion-prod.appdownloadrecord ',$query),ToArray ();Echo' <pre> ';Print_r($rows);Echo' </pre> ';Exit;

7) Group (aggregation) operations

//mysql:select sys,sum (*) as total from Appdownloadrecord WHERE source= ' bdsem1 ' GROUP by SYS;$document= [    ' Aggregate ' = ' appdownloadrecord ', ' Pipeline ' [        [            ' $match ' = [                ' Source ' = ' bdsem1 '            ]        ],        [            ' $group ' = [                ' _id ' and ' $sys ', ' total ' and ' = ' $sum ' =>1]            ]        ]    ], ' allowdiskuse ' =false,];$command=New\mongodb\driver\command ($document);$rows=$mongo->executecommand (' Promotion-prod ',$command),ToArray ();Echo' <pre> ';Print_r($rows);Echo' </pre> ';Exit;

8) Group has

//mysql:select sys,sum (*) as total from Appdownloadrecord WHERE source= ' bdsem1 ' GROUP by SYS has TOTAL>1000;$document= [    ' Aggregate ' = ' appdownloadrecord ', ' Pipeline ' [        [            ' $match ' = [                ' Source ' = ' bdsem1 '            ]        ],        [            ' $group ' = [                ' _id ' and ' $sys ', ' total ' and ' = ' $sum ' =>1]            ]        ],        [            ' $match ' = [                ' Total ' = [' $gt ' =>1000]            ]        ]    ], ' allowdiskuse ' =false,];$command=New\mongodb\driver\command ($document);$rows=$mongo->executecommand (' Promotion-prod ',$command),ToArray ();Echo' <pre> ';Print_r($rows);Echo' </pre> ';Exit;

9) Sorting sort

//Mysql:select * from Appdownloadrecord WHERE source= ' bdsem1 ' ORDER by createtime desc LIMIT;$filter= [' Source ' = ' bdsem1 '];$options= [    ' sort ' = = [' Createtime ' =>-1], ' limit ' = 10];$query=New\mongodb\driver\query ($filter,$options);$rows=$mongo->executequery (' Promotion-prod.appdownloadrecord ',$query),ToArray ();Echo' <pre> ';Print_r($rows);Echo' </pre> ';Exit;

Limit paging

//Mysql:select * from Appdownloadrecord WHERE source= ' bdsem1 ' LIMIT;$filter= [' Source ' = ' bdsem1 '];$options= [    ' Limit ' = 10];$query=New\mongodb\driver\query ($filter,$options);$rows=$mongo->executequery (' Promotion-prod.appdownloadrecord ',$query),ToArray ();Echo' <pre> ';Print_r($rows);Echo' </pre> ';Exit;
//Mysql:select * from Appdownloadrecord WHERE source= ' bdsem1 ' LIMIT 10,10;$filter= [' Source ' = ' bdsem1 '];$options= [    ' Skip ' =, ' limit ' = 10];$query=New\mongodb\driver\query ($filter,$options);$rows=$mongo->executequery (' Promotion-prod.appdownloadrecord ',$query),ToArray ();Echo' <pre> ';Print_r($rows);Echo' </pre> ';Exit;

Finish.

PHP MongoDB manager various poses for data checking

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.