zoukankan      html  css  js  c++  java
  • MongoDB shell基本操作

    shell命令操作语法和JavaScript很类似,其实控制台底层的查询语句都是用JavaScript脚本完成操作的。使用shell 命令,需要启动mongo.exe。mongodb百科

    常用shell命令如下:

    1、查询本地所有数据库名称  

    > show dbs;

    2、切换至指定数据库环境(若无指定的数据库,则创建新的库)

    > use mydb;

    3、查询当前库下的所有聚集集合collection(相当于table)

    > show collections;

    4、创建聚集集合

    > db.createCollection('mycollection');

    5、查询聚集集合中数据条数

    > db.mycollection.count();

     6、插入数据

    > db.mycollection.insert({'username':'xyz_lmn','age':26,'salary':120});

    往'mycollection'聚集集合中插上一条数库,name为'xyz_lmn',age为'26',salary为'120'

    7、查询age等于26的数据

    > db.mycollection.find({"age":26});

    8、查询salary大于100的数据

    > db.mycollection.find({salary:{$gt:100}});

    9、查询age小于30,salary大于100的数据

    > db.mycollection.find({age:{$lt:30}},{salary:{$gt:100}});

    10、查询salary小于40或salary大于200的数据

    > db.mycollection.find({$or: [{salary: {$lt:40}}, {salary: {$gt:200}}]});

    11、查询指定列的数据

    > db.mycollection.find({},{age:1,salary:1});

    1表示显示此列的意思,也可以用true表示
         

    12、查询username中包含'e'的数据

    > db.mycollection.find({username:/e/});

    13、查询以a打头的数据

    > db.mycollection.find({username:/^a/});

    14、查询age列数据,并去掉重复数据

    > db.mycollection.distinct('age');

    15、查询前10条数据

    > db.mycollection.find().limit(10);

    16、查询1条以后的所有数据

    > db.mycollection.find().skip(1);

    17、查询第一条数据

    > db.mycollection.findOne();

    18、查询结果集的记录数(查询salary小于40或大于100的记录数)

    db.mycollection.find({$or: [{salary: {$lt:40}}, {salary: {$gt:100}}]}).count();

    19、按salary升序排序

    > db.mycollection.find().sort({salary:1});

    按照salary字段升序排序

    20、降序

    > db.mycollection.find().sort({salary:-1});

    按照salary字段降序排序

    21、根据username修改age

    > db.employee.update({username:'jim'},{$set:{age:22}},false,true);

    db.collection.update( criteria, objNew, upsert, multi )

    criteria : update的查询条件,类似sql update查询内where后面的
    objNew   : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
    upsert   : 如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
    multi    : mongodb默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。

    22、将指定username的age字段增加5

    > db.mycollection.update({username:'jim'},{$inc:{age:5}},false,true);

    将username为‘jim’的age字段加5

    23、删除username为'rose'的数据

    > db.mycollection.remove({uname:'rose'});


     24、集合collection重命名

    > db.mycollection.renameCollection('c_temp');

    将mycollection集合重命名为'c_temp'

    25、删除集合

    > db.c_temp.drop();

    删除名为'c_temp'的集合

    26、删除当前数据库

    > db.dropDatabase();

    原文链接:http://blog.csdn.net/xyz_lmn/article/details/8072621

  • 相关阅读:
    git的工作区和暂存区
    git的撤销、删除和版本回退
    PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/phalcon.so'
    please complete all spokes before continuing 提示
    右键添加git bush here
    phalcon 设置cookie一直是httponly导致前端读取不到cookie的值
    composer update 提示 username
    Git Error: warning: refname 'origin/branch-name' is ambiguous.
    Phalcon框架数据库读写分离的实现方法
    Windows环境下redis 配置文件中设置的密码无效
  • 原文地址:https://www.cnblogs.com/web-easy/p/7856685.html
Copyright © 2011-2022 走看看