mongodb 知识点1.txt

    xiaoxiao2022-07-12  159

    1.基本数据类型: null: 空值或者不存在的字段 {"x" : null} 布尔型:true, false {"x" : true} 数值:shell默认使用64位浮点型数值 {"x" : 3.14} {"x" : 3} 字符串:UTF-8字符串都可表示为字符串类型的数据 {"x" : "test"} 日期:被存储为自新纪元以来经过的毫秒数,不存储时区 {"x" : new Date()} 数组:既能作为有序对象,也能作为无序对象操作,可包含不同数据类型的元素 {"things" : ["pie", 3.14]} 【注意】mongodb区分字母大小写 2.非关系型数据库的结构单元: 文档 -> 集合 -> 非关系型数据库 多个文档组成集合,多个集合组成数据库 类比: 行(元组) -> 表 -> 关系型数据库 3.集合命名规范: 不能是空字符串 不能包含\0字符,这种字符标识集合名结束 不能以"system."开头,这是系统集合保留的前缀 不能包含保留字符.和$ 4.数据库命名规范: 不能是空字符串 只能包含字母,数字,下划线 区分大小写,通常最好全部小写 最多为64字节 5.创建数据库: use 数据库名 例子: use mydb 查看当前有哪些数据库: show dbs 展示当前使用的数据库: db 往当前数据库中插入一条文档 var m = {"name":"tutorials point"} db.movie.insert(m) 或者: db.movie.insert({"name":"tutorials point"}) 【注意】 1.空的数据库不显示出来,只要往里面插入一条文档后,才会显示出来。 2.mongodb默认连接的数据库是test 6.创建集合: db.createCollection(集合名) 例子: use test db db.createCollection("mycollection") 查看当前数据库中有哪些集合: show collections 【常用】 在mongoDB中,不必显示的创建集合,当插入文档时,集合会被自动创建 7.各种删除 删除当前数据库: db.dropDatabase() 删除集合: db.集合名.drop() 删除一条文档: db.mycol.remove({'title':'MongoDB Overview'}) 删除所有文档: db.mycol.remove({}) 例子: use test show collections db.mycollection.drop() show collections 8.插入文档 db.集合名.insert(文档) 插入一条文档: db.mycol.insert({ title:'MongoDB Overview', by:'tutorials point', tags:['mongodb','database','NoSQL'], likes:100, price:90 }) 插入多条文档: db.mycol.insert([ { title:'MySQL Overview', by:'tutorials point', tags:['mysql','database'], likes:130, price:70 }, { title:'Network Overview', by:'renming', tags:['python','internet'], likes:20, price:110 } ]) 9.查询文档 查看某集合下的所有文档: db.集合名.find() 例子: db.mycol.find() 10.用pretty()格式化展示的结果 db.集合名.find().pretty() 例子: db.mycol.find().pretty() 11.findOne()查询集合中的一条文档 db.集合名.findOne() 例子: db.mycol.findOne() 12.删除文档remove() db.集合名.remove(删除条件,[true/false]) true 或者 1 :只删除满足条件的一条文档 删除一条文档: db.mycol.remove({'title':'MongoDB Overview'}) db.mycol.find() # 查看全部。 删除所有文档 db.mycol.remove({}) # {}不可以省略。 db.mycol.find().pretty() 13.find()函数条件查找 find()函数可以加上表示满足条件的参数,条件用字典的形式表示。 例子: db.mycol.find({"by":"tutorials point"}).pretty() db.mycol.find({"likes":{$lt:50}}).pretty() #$lt:less than < db.mycol.find({"likes":{$lte:50}}).pretty()#$lte:less than equal<= db.mycol.find({"likes":{$gt:50}}).pretty() #$gt:greater than > db.mycol.find({"likes":{$gte:50}}).pretty()#$gte:>= db.mycol.find({"likes":{$ne:50}}).pretty() #$ne:not equal != and:在find()中,如果你传入多个用逗号分隔的键,那么即视为and条件, 示例如下: 查找由tutorials point出版的,并且likes等于100的书的信息 db.mycol.find({'by':'tutorials point','likes':100}).pretty() db.集合名.find( { $and: [ {key1:value1},{key2:value2} ] } ).pretty() 例子: db.mycol.find( { $and:[ {'by':'tutorials point'},{'likes':100} ] } ).pretty() 对比find()加不加$and的情况: 1. 当并列判断条件涉及到同一个字段2次及以上时,必须用and。 示例: db.mycol.find( { $and:[ {'likes':{$gt:20}},{'likes':{$lt:130}} ] } ).pretty() 2. db.mycol.find({'likes':{$gt:20}}) db.mycol.find({'likes':{$lt:130}}) 【注意】 find()函数中,需要满足的搜索条件中, 如果是对同一个属性(键)进行筛选,必须用完整的$and; 如果对不同的属性(键)进行筛选,则可以省略,用简单的写法。 练习: 1. 查看mycol中,price为90,并且,likes为100的文档有哪些 db.mycol.find({'price':90','likes':100}) 2. 查看mycol中,price大于70,并且,price小于110的文档有哪些 db.mycol.find( { $and:[ {'price':{$gt:70}},{'price':{$lt:110}} ] } ).pretty() or:多个条件,满足任意其一 db.mycol.find({ $or:[ {key1:value1},{key2:value2} ] }).pretty() 例子: db.mycol.find({ $or:[ {'price':90},{likes:20} ] }).pretty() 练习: 1.查出mycol中,tutorials point出版的,或者书名为MongoDB Overview的文档信息 2.查出mycol中,likes小于30,或者likes大于100的文档 in 和 not in db.mycol.find({ title: { $in: ["MongoDB Overview","MySQL Overview"] } }) db.mycol.find({ title: { $nin: ["MongoDB Overview","MySQL Overview"] } }) 练习: 查找mycol中,likes是20或者130的文档,要求用$in完成 db.mycol.find({ likes: { $in: [20,130] } }).pretty() 14.更新文档update(),$set,$unset 语法: db.集合名.update({满足的条件},{更新的新内容}) 示例: post = {name:"Joe", age:30, sex:"M", location:"Beijing"} db.users.insert(post) db.users.find().pretty() $set指定的字段,存在,则修改它;不存在,则创建它。 "favorite book"的引号必须有。 db.users.update({name:"Joe"},{$set:{"favorite book":"war and peace"}}) $unset用于删除键: db.users.update({name:"Joe"},{$unset:{"favorite book":1}}) db.users.update({name:"Joe"},{$set:{"favorite book":["war and peace","python overview"]}}) db.users.find().pretty() 【注意】 db.users.update({"name":"Joe"},{"favorite book":"war and peace"}) 不是修改favorite book的值,而是用文档{"favorite book":"war and peace"}代替原来的全部文档。 修改键值,一定要用$set。 15.增加和减少$inc $inc用来增加已有的键的值,如果该键不存在,就创建一个。 $inc只能用于数值类型。 db.transcript.insert({"name":"Bob"}) db.transcript.update({"name":"Bob"},{$inc:{"score":20}}) db.transcript.find() db.transcript.update({"name":"Bob"},{$inc:{"score":-30}}) 16.更新多个文档{multi:true} 默认update()只改变一条文档的值。要改变多个文档,需要用参数'multi' db.mycol.update({"by":"tutorials point"},{$inc:{"price":10}},{multi:true}) 17.删除数组的元素。$pop, $pull 1)$pop 从数组的一端删除元素。 {$pop: {键:1}} 从数组末尾删除一个元素。 {$pop: {键:-1}}从数组头部删除一个元素。 示例: db.lists.insert({"todo":["dishes","laundry","cleaning","cooking"]}) db.lists.find().pretty() db.lists.update({},{$pop:{"todo":1}}) db.lists.find().pretty() db.lists.update({},{$pop:{"todo":-1}}) db.lists.find().pretty() 2)$pull通过指定数组中元素的值来删除该元素。 db.lists.update({},{$pull:{"todo":"laundry"}}) db.lists.find().pretty() db.lists.update({},{$unset:{"todo":1}}) db.lists.find().pretty() 18.投影find() 语法: db.集合名.find({},{键1:0}) 描述: 选择一条或多条文档中有用的数据。 find()限制所选择的键,1(默认)表示显示该键,0表示隐藏该键。 示例: db.mycol.find({},{_id:0,title:1,price:1}) 把mycol中,price大于80元的文档中的title和price打印出来 db.mycol.find({"price":{$gt:80}},{_id:0,title:1,price:1}) 把mycol中,由tutorials point出版或者title是MongoDB Overview的文档, 并且,likes>10,满足以上条件的文档选中,只列出title,by,likes三个属性 提示:find,$or,$and可用可不用,投影 db.mycol.find({ $or:[ {"by":"tutorials point"}, {"title":"MongoDB Overview"} ], "likes":{$gt:10} },{_id:0,title:1,by:1,likes:1}) 19.limit() 语法: db.集合名.find().limit(num) 描述: 此函数用于,限制查询结果的显示条数。 num, 最多显示num条。 示例: db.mycol.find().limit(2)
    最新回复(0)