Home php教程 php手册 php对mongodb的扩展(初出茅庐)

php对mongodb的扩展(初出茅庐)

Jun 13, 2016 am 11:56 AM
mongodb mysql php right us Expand of

我们的php mongodb也能做mysql、sqlserver能做的几乎所有功能,本文将详细介绍

一、操作符
操作符相信大家肯定都知道了,就是等于、大于、小于、不等于、大于等于、小于等于,但是在mongodb里不能直接使用这些操作符。在mongodb里的操作符是这样表示的:
(1) $gt > (大于)   
(2) $lt  (3) $gte  >= (大于等于)
(4) $lt  (5) $ne  != (不等于) 
(6) $in  in (包含)      
(7) $nin  not in (不包含)  
(8) $exists  exist (字段是否存在) 
(9) $inc  对一个数字字段field增加value
(10) $set  就是相当于sql的set field = value
(11) $unset  就是删除字段  
(12) $push  把value追加到field里面去,field一定要是数组类型才行,如果field不存在,会新增一个数组类型加进去
(13) $pushAll  同$push,只是一次可以追加多个值到一个数组字段内
(14) $addToSet  增加一个值到数组内,而且只有当这个值不在数组内才增加。
(15) $pop  删除最后一个值:{ $pop : { field : 1 } }删除第一个值:{ $pop : { field : -1 } }注意,只能删除一个值,也就是说只能用1或-1,而不能用2或-2来删除两条。mongodb 1.1及以后的版本才可以用
(16) $pull  从数组field内删除一个等于value值
(17) $pullAll  同$pull,可以一次删除数组内的多个值
(18) $ 操作符  是他自己的意思,代表按条件找出的数组里面某项他自己。这个比较坳口,就不说了。

二、CURD 增、改、读、删
增加

复制代码 代码如下:


db.collection->insert({'name' => 'caleng', 'email' => 'admin#admin.com'});


是不是灰常简单呀,对就是这么简单,它没有字段的限制,你可以随意起名,并插入数据

修改

复制代码 代码如下:


db.collection.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } ); 只更新了第一条大于1记录
db.collection.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true ); 大于3的记录 全更新了
db.collection.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false ); 大于4的记录 只加进去了第一条
db.collection.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true ); 大于5的记录 全加进去


查询

复制代码 代码如下:


db.collection.find(array('name' => 'bailing'), array('email'=>'email@qq.com'))
db.collection.findOne(array('name' => 'bailing'), array('email''email@qq.com'))


大家可以看到查询我用了两种不同的写法,这是为什么,其实这跟做菜是一样的,放不同的调料,炒出的菜是不同的味道。下面给大家说一下,这两种调料的不同作用。
findOne()只返回一个文档对象,find()返回一个集合列表。
也就是说比如,我们只想查某一条特定数据的详细信息的话,我们就可以用findOne();
如果想查询某一组信息,比如说一个新闻列表的时候,我们就可以作用find();
那么我想大家这时一定会想到我想对这一个列表排序呢,no problem mongodb会为您全心全意服务

复制代码 代码如下:


db.collection.find().sort({age:1}); //按照age正序排列
db.collection.find().sort({age:-1}); //按照age倒序排列
db.collection.count(); //得到数据总数
db.collection.limit(1); //取数据的开始位置
db.collection.skip(10); //取数据的结束位置
//这样我们就实现了一个取10条数据,并排序的操作。


删除
删除有两个操作 remove()和drop()

复制代码 代码如下:


db.collection.remove({"name",'jerry'}) //删除特定数据
db.collection.drop() //删除集合内的所有数据


distinct操作

复制代码 代码如下:


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


噢!一口气写太多了,大家看太多也不易消化。今天就到这里吧,明天接着写php对mongodb的操作,尽请期待哦!不能再写了,不然的话明天会变熊猫。good night. have a good dream.
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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months 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)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

How to connect to mongodb How to connect to mongodb Apr 12, 2025 am 09:09 AM

To connect to MongoDB with Navicat: Install Navicat and create a MongoDB connection; enter the server address in the host, enter the port number in the port, and enter the MongoDB authentication information in the user name and password; test the connection and save; Navicat will connect to the MongoDB server.

See all articles