Home Backend Development PHP Tutorial PHP extension to mongodb (fledgling)_PHP tutorial

PHP extension to mongodb (fledgling)_PHP tutorial

Jul 21, 2016 pm 03:14 PM
mongodb mysql php right us Expand of

Our php mongodb can also do almost all the functions that mysql and sqlserver can do. This article will introduce in detail

1. Operators
I believe everyone must know it, that is Equal to, greater than, less than, not equal to, greater than or equal to, less than or equal to, but these operators cannot be used directly in mongodb. The operator in mongodb is expressed like this:
(1) $gt > (greater than) 
(2) $lt Equal to)
(4) $lt  <= (less than or equal to) 
(5) $ne != (not equal to) 
(6) $in in (inclusive)  nin not in (not included) 
(8) $exists exist (whether the field exists) 
(9) $inc Add value to a numeric field field
(10) $set It is equivalent to the set of sql field = value
(11) $unset It is to delete the field
(12) $push Append the value to the field. The field must be an array type. If the field does not exist, a new array type will be added. Go in
(13) $pushAll Same as $push, except that multiple values ​​can be appended to an array field at one time
(14) $addToSet Adds a value to the array, and only adds it if the value is not in the array .
(15) $pop Delete the last value: { $pop : { field : 1 } } Delete the first value: { $pop : { field : -1 } } Note that only one value can be deleted, that is It says that you can only use 1 or -1, but not 2 or -2 to delete two items. Only mongodb 1.1 and later versions can use
(16) $pull to delete a value equal to value from the array field
(17) $pullAll Same as $pull, you can delete multiple values ​​in the array at one time
(18) The $ operator is his own meaning, representing himself to find an item in the array based on conditions. This one is more difficult, so I won’t talk about it.


2. CURD Add, modify, read, delete
Add

Copy code The code is as follows:
db.collection->insert({'name' => 'caleng', 'email' => 'admin#admin.com'});



Isn’t it very simple? Yes, it is that simple. It has no field restrictions. You can name it as you like and insert data


Modify

Copy code The code is as follows:
db.collection.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } ); Only the first record greater than 1 is updated
db.collection.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK "} },false,true ); All records greater than 3 are updated
db.collection.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK" } },true,false ); Records greater than 4 are only added to the first
db.collection.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true ); Add all records greater than 5


Query

Copy code The code is as follows:
db.collection.find(array('name' => 'bailing'), array('email'=>'email@qq.com' ))
db.collection.findOne(array('name' => 'bailing'), array('email''email@qq.com'))


You can see In the query, I used two different ways of writing it. This is why. In fact, it is the same as cooking. Adding different seasonings will result in different flavors in the dishes. Let me tell you the different functions of these two seasonings.
findOne() only returns a document object, and find() returns a collection list.
That is to say, for example, if we only want to check the detailed information of a specific piece of data, we can use findOne();
If we want to query a certain set of information, such as a news list, we can It can be used as find();
Then I think everyone will think that I want to sort this list. No problem mongodb will serve you wholeheartedly


Copy code The code is as follows:
db.collection.find().sort({age:1}); //Arrange in positive order according to age
db.collection.find( ).sort({age:-1}); //Arrange in reverse order by age
db.collection.count(); //Get the total number of data
db.collection.limit(1); //Get data The starting position of
db.collection.skip(10); //Get the end position of the data
//In this way, we have implemented an operation of taking 10 pieces of data and sorting them.


Delete
Deletion has two operations remove() and drop()

Copy code The code is as follows:
db.collection.remove({"name",'jerry'}) //Delete specific data
db.collection.drop() //Delete the data in the collection All data


distinct operation
Copy code The code is as follows:

db.user.distinct('name', {'age ': {$lt : 20}})

Oh! It’s too much to write in one breath, and it’s hard for everyone to digest after reading too much. That’s it for today. Tomorrow I will write about the operation of php on mongodb. Please look forward to it! I can’t write anymore, otherwise I will turn into a panda tomorrow. good night. have a good dream.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326319.htmlTechArticleOur php mongodb can also do almost all the functions that mysql and sqlserver can do. This article will introduce in detail 1. Operation I believe everyone must know the operators, which are equal to, greater than, less...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

CakePHP Useful Resources CakePHP Useful Resources Sep 10, 2024 pm 05:27 PM

The following resources contain additional information on CakePHP. Please use them to get more in-depth knowledge on this.

See all articles