1024programmer Mongodb Method of operating MongoDB database in PHP program

Method of operating MongoDB database in PHP program

<?php
error_reporting(7);
$conn = new Mongo();
$db = $conn->PHPDataBase;
$collection = $db->PHPCollection;
/*—————————–
* delete
*—————————–
$collection->remove(array(“name” => “xixi111”));
*/
/*——————————
* Insert
*——————————
for($i = 0; $i <= 50; $i++) {
$data = array(“name” => “xixi”.$i,”email” => “673048143_”.$i.”@qq.com”, “age” => $i*1+20);
$collection->insert($data);
}
*/
/*——————————-
* Find
*——————————–
$res = $collection->find(array(“age” => array(‘$gt’ => 25,’$lt’ => 40)), array(“name” => true));
foreach($res as $v) {
print_r($v);
}
*/
/*——————————-
* Update
*——————————–
$collection->update(array(“age” =>22),array(‘$set’ => array(“name” => “demoxixi”)));
*/
?>
PHP operation MongoDB

Complete the installation of MongoDB

Complete the extension of PHP to MongoDB and restart apache

Start the MongoDB service, just like mysql.

Then you can operate MongoDB with PHP.

Here are some notes from my own study today:

First of all, you must have a clear understanding of the concept.
Additions, deletions, revisions and inquiries of news.
The first is news addition, news title, news author, and news content.
Then read the news. The data in MongoDB is read out and read through PHP.
The interface here is written in HTML language, this is the same.

A traditional relational database generally consists of three levels: database, table, and record.
MongoDB is composed of three levels: database, collection, and document.
MongoDB uses a document-oriented data model that allows it to freely split data among multiple servers.
It also balances data and load across the cluster, automatically rearranging documents.

Support:
Index
Can store Javascript
file storage

Not supported:
joins and complex multi-row transactions

A document is the basic unit of data in MongoDB (similar to a row in a relational database)
Similarly, collections can be thought of as tables without a schema.

A document is an ordered sequence of keys and their associated values.
{“greeting”:”Hello, world!”}
This document has only one key “greeting”, and its corresponding value is “Hello, world!”
{“greeting”:”Hello, world!”,”foo”:3}

MongoDB is not only type sensitive, but also case sensitive:
{“foo”:3}
{“foo”:”3″}
and
{“foo”:3}
{“Foo”:3}
are different

One more very important thing, MongoDB documents cannot have duplicate keys. The following documents are illegal:
{“greeting”:”Hello, world!”,”greeting”:”Hello, MongoDB”}

collection
A collection is a group of documents. If documents in MongoDB are like rows in a relational database, collections are like tables.

Collections are schemaless, which means that documents in a collection can be of various types.
For example the following documents can exist in the same collection:
{“greeting”:”Hello, world!”}
{“foo”:5}

Put documents of the same type in a collection, so that the data will be more concentrated.

Multiple documents in MongoDB form a collection, and multiple collections can also form a database.

The database name will eventually become a file in the file system.

CRUD
Insert:
The insert function adds a document to the collection. Example: db.blog.insert(post).
Read:
find reads the documents in the collection. Example: db.blog.find().
If you want to view a document, you can use findOne.
Update:
update. update accepts two parameters, the first is the qualification of the document to be updated, and the second is the new document.
Example: db.blog.update({title:”My Blog Post”},post)
delete:
remove. It can accept a document to specify constraints.
db.blog.remove({title:”My Blog Post”})

MongoDB field type
null:
null is used to represent an empty value or a field that does not exist. {“x”:null}
Boolean:
Boolean type has two values ​​’true’ and ‘false’:
{“x”:true}

32-bit integer:
64-bit integers:

64-bit floating-point numbers: All numbers in the shell are of this type.
{“x”:3.14}
This is also a floating point number
{“x”:3}

String:
{“x”:”foobar���}
Symbol:
Object id:
Date:
Regular expression:
{“x”:/foobar/i}
Code:
{“x”:function(){/*…*/}}
Binary data:
Maximum value:
Min:
undefined:
{“x”:undefined}
Array:
{“x”:[“a”,”b”,”c”]}
Embedded documents:
{“x”:{“foo”:”bar”}}

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/method-of-operating-mongodb-database-in-php-program/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索