0
点赞
收藏
分享

微信扫一扫

2-1 MongoDB 文档的高级查询操作--educoder上面的nosql题目以及笔记总结

芝婵 2022-04-04 阅读 47
mongodbnosql

2-1 MongoDB 文档的高级查询操作

第一关:数据的导入导出   未连接客户端时使用

数据导入工具:mongoimport

-d Testdb1 :指定将数据导入到 Testdb1 数据库;

-c score :将数据导入到集合 score ,如果这个集合之前不存在,会自动创建一个(如果省略 –collection 这个参数,那么会自动新建一个以  CSV 文件名为名的集合);

–type csv :文件类型,这里是 CSV;

–headerline :这个参数很重要,加上这个参数后创建完成后的内容会以 CSV 文件第一行的内容为字段名(导入json文件不需要这个参数);

–ignoreBlanks :这个参数可以忽略CSV文件中的空缺值(导入json文件不需要这个参数);

–file 1.csv :这里就是 CSV 文件的路径了,需要使用绝对路径。

示例:

导入数据

数据库导出工具:mongoexport

 

导出json:

-o /file.json :输出的文件路径/(根目录下)和文件名;

–type json :输出的格式,默认为 json

导出csv:

-f :当输出格式为 csv 时,需要指定输出的字段名

编程测试代码:

mongoimport -d mydb1 -c test --type csv --headerline --ignoreBlanks --file /home/example/student.csv

mongoexport -d mydb1 -c test -o /home/test1.json --type json

mongoexport -d mydb1 -c test -o /home/test1.csv --type csv -f "_id,name,age,sex,major"

第二关: 高级查询(一)

相关知识:

$all: 会查询满足方括号中所有条件的文档,如果只有其中一项满足是不会被查询出来的

$exists: 判断字段是否存在

$mod: 取模运算  db.hobbies.find({age:{$mod:[7,4]}})

$in: 包含

$nin: 不包含

$size: 数组元素个数

一般代码:db.集合.find({属性:{$匹配词:{需要满足的条件}  }}).pretty()

示例

mongo

use db1

document=([

{_id:1,name:"小红",sex:"女",hobbies: ["唱歌","跳舞","羽毛球"]},

{_id:2,name:"小明",sex:"男",hobbies: ["唱歌","乒乓球","羽毛球"]},

{_id:3,name:"小亮",sex:"男",hobbies: ["乒乓球","羽毛球"]}]

)

db.hobbies.insert(document)

 

$all 匹配所有

db.hobbies.find({hobbies:{$all:["唱歌","羽毛球"]}})

$exists 判断字段是否存在

db.hobbies.save({_id:1,name:"小红",age:18,sex:"女",hobbies:["唱歌","跳舞","羽毛球"]})

db.hobbies.find({age:{$exists:true}})

db.hobbies.find({age:{$exists:false}})

$mod 取模运算

db.hobbies.update({_id:2},{$set:{age:20}})     #添加小明年龄20

db.hobbies.update({_id:3},{$set:{age:22}})     #添加小亮年龄22

db.hobbies.find({age:{$mod:[7,4]}})

$in 包含  nin 不包含

db.hobbies.find({age:{$in:[17,20]}})

db.hobbies.find({age:{$nin:[17,20]}})

 

$size 数组元素个数

db.hobbies.find({hobbies:{$size:2}})

 

查询结果排序

db.hobbies.find({age:{$nin:[17,20]}}).sort({_id:1})     #将查询结果按照_id升序排序

db.hobbies.find({age:{$nin:[17,20]}}).sort({_id:-1})    #将查询结果按照_id降序排序

 

编程测试代码:

mongoimport -d mydb2 -c test --type json --file /home/example/person.json

mongo

use mydb2

查询

db.test.find({hobbies:{$all:["唱歌","跳舞"]}}).sort({_id:1})

db.test.find({hobbies:{$all:["羽毛球","跳舞"]}}).sort({_id:1})

db.test.find({hobbies:{$size:3}}).sort({_id:1})

db.test.find({"hobbies":{$exists:true}}).sort({_id:1})

db.test.find({age:{$in:[19,21]}}).sort({_id:1})

db.test.find({age:{$nin:[20]}}).sort({_id:1})

db.test.find({age:{$mod:[9,2]}}).sort({_id:1})

第三关:高级查询(二)

       $or: 条件之间的或查询

       $and: 条件之间的且查询

       $not: 条件取反查询

一般查询代码:db.集合.find({$or/and/not:{属性:{条件}}})

or/and 在前面,是联系两个或多个查询条件

示例:

mongo

use db3

document=([

{_id:1, name:"王小丽",age: 19, sex:"女",major: "计算机"},

{_id:2, name:"张明",age: 21, sex:"男",major: "计算机"},

{_id:3, name:"秋雅",age: 20, sex:"女",major: "播音主持"},

{_id:4, name:"张欣欣",age: 18, sex:"女",major: "表演"}])

db.student.insert(document)

 

$or 条件之间的或查询

$or 表示多个查询条件之间是或的关系,比如查询性别 sex 为 男 或年龄 age 为18的文档信息:

db.student.find({$or:[{sex:"男"},{age:18}]})

 

$and 条件之间的且查询

$and表示多个查询条件之间是且的关系,比如查询年龄 age 大于18且小于21(18 < age < 21)的信息:

db.student.find({$and:[{age:{$gt:18}},{age:{$lt:21}}]})

 

$not 条件取反查询

$not 用来执行取反操作,比如查询年龄 age 大于等于20岁,然后进行取反(即查询年龄小于20岁的文档):

db.student.find({age:{$not:{$gte:20}}})

 

正则表达式匹配查询

查询不符合major=计*开头文档:

db.student.find({major:{$not:/^计.*/}})

 

count() 返回结果集总数

比如返回上一步正则查询到的结果集有几条:

db.student.find({major:{$not:/^计.*/}}).count()

 

编程测试代码:

mongoimport -d mydb3 -c test --type json /home/example/person.json

mongo

use mydb3

db.test.find({$and:[{age:20},{sex:"男"}]}).sort({_id:1})

db.test.find({$or:[{age:20},{sex:"男"}]}).sort({_id:1})

db.test.find({name:/^韩.*/}).sort({_id:1})

db.test.find({$and:[{age:{$gte:19}},{age:{$lt:22}}]}).sort({_id:1})

db.test.find({$or:[{age:{$lt:19}},{age:{$gt:21}}]}).sort({_id:1})

db.test.find({name:{$not:/^韩.*/}). sort({_id:1})

db.test.find({name:{$not:/^韩.*/}}).count()

db.test.find({$and:[{age:{$gte:19}},{age:{$lt:22}}]}).count()\

第四关:游标

什么是游标

通俗的说,游标不是查询结果,而是查询的返回资源,或者接口。通过这个接口,你可以逐条读取。就像 fopen 打开文件,得到一个资源一样,通过资源,可以一行一行的读文件。

使用循环插入数据

我们首先插入10000条数据到集合 items,因为 mongodb 底层是 javascript 引擎,所以我们可以使用 js 的语法来插入数据:

for(var i=0;i<10000;i++)db.items.insert({_id:i,text:"Hello MongoDB"+i})

show collections

db.items.find().count()         计数

db.items.find().limit(10)    输出前十

 

声明游标

定义一个变量来保存这个游标,find 的查询结果(_id<=5)赋值给了游标 cursor 变量,代码如下:

var cursor=db.items.find({_id:{$lte:5}})

打印游标中的数据信息

有四种方法进行打印:

1、printjson(cursor.next()) 打印下一条数据  

注意:当取完游标中的数据,又进行打印,会报错

 

2、使用 js 的 while 语法来循环打印

var cursor=db.items.find({_id:{$lte:5}})

while(cursor.hasNext()){printjson(cursor.next());}

 

3、使用 for 循环打印

var cursor=db.items.find({_id:{$lte:5}})

for(cursor;cursor.hasNext();){printjson(cursor.next());}

4、使用 forEach 打印

var cursor=db.items.find({_id:{$lte:5}})

cursor.forEach(function(obj){printjson(obj)})

 

在分页的情况下使用游标

假设每页有10行,我们查询第701页,可以配合 skip() 和 limit() 来实现

var cursor=db.items.find().skip(7000).limit(10)

printjson(cursor.toArray()[3])

var cursor=db.items.find().skip(7000).limit(10)

 

cursor.forEach(function(obj){printjson(obj)})

测试编程代码

mongo

use mydb4

for(var i=0;i<10000;i++)db.test.insert({_id:i,title:"MongoDB"+i,content:"hello"+i})

exit

mongoexport -d mydb4 -c test -o /home/test/test4.csv --type csv -f "_id,title,content"

举报

相关推荐

0 条评论