search
HomeDatabaseMysql TutorialMongodb之(初出茅庐)

Mongodb之(初出茅庐) 首先,感谢cnblogs TV,感谢博主的TV,更感谢对上期分享支持的朋友们。在你们的支持,才有了让我写下这一期的动力。夜晚也许你在电视机边看电视,也许你在网上看电影。而我在坚守了一天的电脑之后,继续坐到了电脑边,听着北京广播网的87.


Mongodb之(初出茅庐)

 

首先,感谢cnblogs TV,感谢博主的TV,更感谢对上期分享支持的朋友们。在你们的支持,才有了让我写下这一期的动力。夜晚也许你在电视机边看电视,也许你在网上看电影。而我在坚守了一天的电脑之后,继续坐到了电脑边,听着北京广播网的87.6,写着技术分享。好了重点来了,也是very important.接着说我们的mongodb吧

  www.2cto.com  

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
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
What are the differences in syntax between MySQL and other SQL dialects?What are the differences in syntax between MySQL and other SQL dialects?Apr 27, 2025 am 12:26 AM

MySQLdiffersfromotherSQLdialectsinsyntaxforLIMIT,auto-increment,stringcomparison,subqueries,andperformanceanalysis.1)MySQLusesLIMIT,whileSQLServerusesTOPandOracleusesROWNUM.2)MySQL'sAUTO_INCREMENTcontrastswithPostgreSQL'sSERIALandOracle'ssequenceandt

What is MySQL partitioning?What is MySQL partitioning?Apr 27, 2025 am 12:23 AM

MySQL partitioning improves performance and simplifies maintenance. 1) Divide large tables into small pieces by specific criteria (such as date ranges), 2) physically divide data into independent files, 3) MySQL can focus on related partitions when querying, 4) Query optimizer can skip unrelated partitions, 5) Choosing the right partition strategy and maintaining it regularly is key.

How do you grant and revoke privileges in MySQL?How do you grant and revoke privileges in MySQL?Apr 27, 2025 am 12:21 AM

How to grant and revoke permissions in MySQL? 1. Use the GRANT statement to grant permissions, such as GRANTALLPRIVILEGESONdatabase_name.TO'username'@'host'; 2. Use the REVOKE statement to revoke permissions, such as REVOKEALLPRIVILEGESONdatabase_name.FROM'username'@'host' to ensure timely communication of permission changes.

Explain the differences between InnoDB and MyISAM storage engines.Explain the differences between InnoDB and MyISAM storage engines.Apr 27, 2025 am 12:20 AM

InnoDB is suitable for applications that require transaction support and high concurrency, while MyISAM is suitable for applications that require more reads and less writes. 1.InnoDB supports transaction and bank-level locks, suitable for e-commerce and banking systems. 2.MyISAM provides fast read and indexing, suitable for blogging and content management systems.

What are the different types of JOINs in MySQL?What are the different types of JOINs in MySQL?Apr 27, 2025 am 12:13 AM

There are four main JOIN types in MySQL: INNERJOIN, LEFTJOIN, RIGHTJOIN and FULLOUTERJOIN. 1.INNERJOIN returns all rows in the two tables that meet the JOIN conditions. 2.LEFTJOIN returns all rows in the left table, even if there are no matching rows in the right table. 3. RIGHTJOIN is contrary to LEFTJOIN and returns all rows in the right table. 4.FULLOUTERJOIN returns all rows in the two tables that meet or do not meet JOIN conditions.

What are the different storage engines available in MySQL?What are the different storage engines available in MySQL?Apr 26, 2025 am 12:27 AM

MySQLoffersvariousstorageengines,eachsuitedfordifferentusecases:1)InnoDBisidealforapplicationsneedingACIDcomplianceandhighconcurrency,supportingtransactionsandforeignkeys.2)MyISAMisbestforread-heavyworkloads,lackingtransactionsupport.3)Memoryengineis

What are some common security vulnerabilities in MySQL?What are some common security vulnerabilities in MySQL?Apr 26, 2025 am 12:27 AM

Common security vulnerabilities in MySQL include SQL injection, weak passwords, improper permission configuration, and unupdated software. 1. SQL injection can be prevented by using preprocessing statements. 2. Weak passwords can be avoided by forcibly using strong password strategies. 3. Improper permission configuration can be resolved through regular review and adjustment of user permissions. 4. Unupdated software can be patched by regularly checking and updating the MySQL version.

How can you identify slow queries in MySQL?How can you identify slow queries in MySQL?Apr 26, 2025 am 12:15 AM

Identifying slow queries in MySQL can be achieved by enabling slow query logs and setting thresholds. 1. Enable slow query logs and set thresholds. 2. View and analyze slow query log files, and use tools such as mysqldumpslow or pt-query-digest for in-depth analysis. 3. Optimizing slow queries can be achieved through index optimization, query rewriting and avoiding the use of SELECT*.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use