search

CountDistinctGroup 1.Count 请查询persons中美国学生的人数. db.persons.find({country:USA}).count() 2.Distinct 请查询出persons中一共有多少个国家分别是什么. db.runCommand({distinct:persons“, key:country}).values 3.Group 语法: db.runCommand({g

Count+Distinct+Group

1.Count

请查询persons中美国学生的人数.

db.persons.find({country:"USA"}).count()

2.Distinct

请查询出persons中一共有多少个国家分别是什么.

db.runCommand({distinct:"persons“, key:"country"}).values

3.Group

语法:

db.runCommand({group:{

ns:集合名字,

Key:分组的键对象,

Initial:初始化累加器,

$reduce:组分解器,

Condition:条件,

Finalize:组完成器

}})

分组首先会按照key进行分组,每组的 每一个文档全要执行$reduce的方法,

他接收2个参数一个是组内本条记录,一个是累加器数据.

3.1请查出persons中每个国家学生数学成绩最好的学生信息(必须在90以上)

db.runCommand({group:{

ns:"persons",

key:{"country":true},

initial:{m:0},

$reduce:function(doc,prev){

if(doc.m > prev.m){

prev.m = doc.m;

prev.name = doc.name;

prev.country = doc.country;

}

},

condition:{m:{$gt:90}}

}})

3.2在3.1要求基础之上把每个人的信息链接起来写一个描述赋值到m上

finalize:function(prev){

prev.m = prev.name+" Math scores "+prev.m

}

4.用函数格式化分组的键

4.1如果集合中出现键Counrty和counTry同时存在

db.persons.insert({
name:"USPCAT",
age:27,
email:"2145567457@qq.com",
c:89,m:100,e:67,
counTry:"China",
books:["JS","JAVA","EXTJS","MONGODB"]
})

那分组有点麻烦这要如何解决呢?

db.runCommand({group:{
ns:"persons",
$keyf:function(doc){
if(doc.counTry){
return {country:doc.counTry}
}else{
return {country:doc.country}
}
},
initial:{m:0},
$reduce:function(doc,prev){
if(doc.m > prev.m){
prev.m = doc.m;
prev.name = doc.name;
if(doc.country){
prev.country = doc.country;
}else{
prev.country = doc.counTry;
}
}
},
finalize:function(prev){
prev.m = prev.name+" Math scores "+prev.m
},
condition:{m:{$gt:90}}
}})

数据库命令操作

1.命令执行器runCommand

1.1用命令执行完成一次删除表的操作

db.runCommand({drop:"map"})

{

"nIndexesWas" : 2,

"msg" : "indexes dropped forcollection",

"ns" : "foobar.map",

"ok" : 1

}

2.如何查询mongoDB为我们提供的命令

1.在shell中执行 db.listCommands()

2.访问网址http://localhost:28017/_commands,需要先通过

mongod --dbpath d:\app\mongodata --rest命令打开简单的rest API

3.常用命令举例

3.1查询服务器版本号和主机操作系统

db.runCommand({buildInfo:1})

3.2查询执行集合的详细信息,大小,空间,索引等……

db.runCommand({collStats:"persons"})

3.3查看操作本集合最后一次错误信息

db.runCommand({getLastError:"persons"})

固定集合特性

\

2.固定特性

2.1固定集合默认是没有索引的就算是_id也是没有索引的

2.2由于不需分配新的空间他的插入速度是非常快的

2.3固定集合的顺是确定的导致查询速度是非常快的

2.4最适合的是应用就是日志管理喎?http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+My60tL2oucy2qLyvus88L3A+CjxwPiAgICAgICAzLjG0tL2o0ru49tDCtcS5zLaovK+6z9Kqx/O089ChyscxMDC49tfWvdosv8nS1LTmtKLOxLW1MTC49jwvcD4KPHA+ICAgICAgICAgICAgZGIuY3JlYXRlQ29sbGVjdGlvbigmcXVvdDtteWNvbGwmcXVvdDsse3NpemU6MTAwLGNhcHBlZDp0cnVlLG1heDoxMH0pPC9wPgo8cD4gICAgICAgIDMuMrDR0ru49sbVzai8r7rP16q7u7PJucy2qLyvus88L3A+CjxwPiAgICAgICAgICAgICBkYi5ydW5Db21tYW5kKHtjb252ZXJ0VG9DYXBwZWQ6obFwZXJzb25zobEsc2l6ZToxMDAwMDB9KTwvcD4KPHA+NC63tM/yxcXQ8izErMjPysey5cjry7PQ8sXF0PIuPC9wPgo8cD4gICAgICAgIDQuMbLp0a+5zLaovK+6z215Y29sbLKix9K3tM/sxcXQ8jwvcD4KPHA+ICAgICAgICAgICAgIGRiLm15Y29sbC5maW5kKCkuc29ydCh7JG5hdHVyYWw6LTF9KTwvcD4KPHA+NS7OsrK/086x6iy/yc+nc2hlbGyyu9ans9ZqYXZhus1waHC1yMf9tq/Kx9ans9a1x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ZxdW90O2EudHh0obA8L3A+CjxwPiAgICBWVUW/ydLUsum/tCxzaGVsbM7et6i08r+qzsS8/jwvcD4KPHA+My41sum/tMv509DOxLz+PC9wPgo8cD4gICAgbW9uZ29maWxlcyAtZCBmb29iYXIgbGlzdDwvcD4KPHA+My41yb6z/dLRvq205tTatcTOxLz+VlVF1tCy2df3PC9wPgo8cD4gICAgbW9uZ29maWxlcyAtZCBmb29iYXIgZGVsZXRlIA=="a.txt'

服务器端脚本

1.Eval

1.1服务器端运行eval

db.eval("function(name){return name}","uspcat")

2.Javascript的存储

2.1在服务上保存js变量活着函数共全局调用

1.把变量加载到特殊集合system.js中

db.system.js.insert({_id:name,value:”uspcat”})

2.调用

db.eval("return name;")

System.js相当于Oracle中的存储过程,因为value不单单可以写变量

还可以写函数体也就是javascript代码

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
MySQL String Types: Storage, Performance, and Best PracticesMySQL String Types: Storage, Performance, and Best PracticesMay 10, 2025 am 12:02 AM

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

Understanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreUnderstanding MySQL String Types: VARCHAR, TEXT, CHAR, and MoreMay 10, 2025 am 12:02 AM

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

What are the String Data Types in MySQL?What are the String Data Types in MySQL?May 10, 2025 am 12:01 AM

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

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

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.