• MongoDB第一课,shell命令下的增删改查


    查看所有数据库列表

    show dbs

    使用数据库、创建数据库

    use databasename

    如果真的想把这个数据库创建成功,那么必须插入一个数据。

    数据库中不能直接插入数据,只能往集合(collections)中插入数据。不需要创建集合,只需要写点语法:

    db.student.insert({“name”:”xiaoming”});

    db.student  系统发现student是一个陌生的集合名字,所以就自动创建了集合。

    删除数据库,删除当前所在的数据库

    db.dropDatabase();

    查看一个集合的信息:

    1 插入数据

    插入数据,随着数据的插入,数据库创建成功了,集合也创建成功了。

    1         db.student.insert({"name":"xiaoming"});

     

    我们不可能一条一条的insert。所以,我们希望用sublime在外部写好数据库的形式,然后导入数据库:

     

    1         mongoimport --db test --collection restaurants --drop --file primer-dataset.json

    -db test  想往哪个数据库里面导入

    --collection restaurants  想往哪个集合中导入

    --drop 把集合清空

    --file primer-dataset.json  哪个文件

     

    这样,我们就能用sublime创建一个json文件,然后用mongoimport命令导入,这样学习数据库非常方便。

     

    2 查找数据

    查找数据,用findfind中没有参数,那么将列出这个集合的所有文档:

    1         db.restaurants.find()

     

    精确匹配:

    1         db.student.find({"score.shuxue":70});

     

    多个条件:

    1         db.student.find({"score.shuxue":70 , "age":12})

     

    大于条件:

    1         db.student.find({"score.yuwen":{$gt:50}});

     

    或者。寻找所有年龄是9岁,或者11岁的学生

    1         db.student.find({$or:[{"age":9},{"age":11}]});

     

    查找完毕之后,打点调用sort,表示升降排序。

    1         db.restaurants.find().sort( { "borough": 1, "address.zipcode": 1 } )

     


    3 修改数据

    修改里面还有查询条件。你要该谁,要告诉mongo

    查找名字叫做小明的,把年龄更改为16岁:

    1         db.student.update({"name":"小明"},{$set:{"age":16}});

     

    查找数学成绩是70,把年龄更改为33岁:

    1         db.student.update({"score.shuxue":70},{$set:{"age":33}});

     

    更改所有匹配项目:"

    By default, the update() method updates a single document. To update multiple documents, use the multi option in the update() method.

    1         db.student.update({"sex":""},{$set:{"age":33}},{multi: true});

     

    完整替换,不出现$set关键字了:

    1         db.student.update({"name":"小明"},{"name":"大明","age":16});

     

    4 删除数据

     

    1         db.restaurants.remove( { "borough": "Manhattan" } )

     

    By default, the remove() method removes all documents that match the remove condition. Use the justOne option to limit the remove operation to only one of the matching documents.

    1         db.restaurants.remove( { "borough": "Queens" }, { justOne: true } )

    不过这些已经比较老了,最新的写法还是要看官方文档,这部分写的还是很清楚的,阅读起来没有问题 https://docs.mongodb.com/manual/tutorial/remove-documents/

  • 相关阅读:
    Atitit.木马病毒websql的原理跟个设计
    Atitit.cateService分类管理新特性与设计文档说明v1
    Atitit.cateService分类管理新特性与设计文档说明v1
    Atitit.iso格式蓝光 BDMV 结构说明
    Atitit.iso格式蓝光 BDMV 结构说明
    Atitit.复合文档的格式 标准化格式
    Atitit.复合文档的格式 标准化格式
    Atitit.木马病毒强制强行关闭360 360tray.exe的方法
    Atitit.木马病毒强制强行关闭360 360tray.exe的方法
    Atitit.复合文档的格式 标准化格式
  • 原文地址:https://www.cnblogs.com/zhangmingzhao/p/7882686.html
Copyright © 2020-2023  润新知