zoukankan      html  css  js  c++  java
  • mongodb shell入门(一)概览

    mongodb shell入门(一)概览

    mongodb shell是个啥
    mongodb shell是一个与mongodb进行交互的工具,它通过命令行(语法长的和js很像)向mongodb发送你想要执行的命令。
    mongodb shell可以用来:
    (1)查看数据库的内容;
    (2)进行查询测试;
    (3)创建索引;
    (4)进行js脚本维护;
    (5)dba对db的管理;

    启动mongodb shell
    ./bin/mongo

    寻找帮助=>阅读本文,记住这四条命令就成
    > help
    > db.help()
    > db.mycollection.help()
    > db.mycollection.find().help()

    基本命令
    (1)显示所有数据库,类似于mysql的”show databases”
    > show dbs
    (2)切换数据库,和mysql的”use test”(假设数据库的名称叫test)类似
    > use test
    (3)显示所有collections,类似于mysql的”show tables”
    > show collections
    (4)查询collections中的所有记录,类似于mysql的”select * from test”
    > db.test.find()
    (5)插入记录,类似于mysql的”insert into test(name) values (“sara”)”
    > db.test.save({name : “sara”})
    (6)修改记录,类似于mysql的’update test set city=”New York” where name=”sara”‘
    > person = db.test.findOne( { name : “sara” } );
    > person.city = “New York”;
    > db.test.save( person );
    (7)删除记录,类似于mysql的delete from test where name=”sara”
    > db.test.remove({name : “sara”})
    (8)删除collections,类似于mysql的drop table test
    > db.test.drop()
    (9)创建索引
    > db.test.ensureIndex({name : 1})

    重要提示
    (1)mongodb是一个类似kv的系统,所有插入的数据会默认生成一个_id的字段作为key;
    (2)mongodb shell默认以double的方式处理所有数字,如果使用long/int的BSON数据,
    请使用NumberLong:
    > db.test.insert({age:NumberLong(10)})
    > db.test.insert({age:new NumberLong(100)})
    当然,还有其他类型的数据,如Date,BinData
    > Date()
    > db.test.insert({date:new Date()})
    > db.test.insert({bin:new BinData(2,”1234″)})
    看一下成果吧
    > db.test.find()
    { “_id” : ObjectId(“4e397eafc328b5026b86e53d”), “name” : “sara”, “city” : “New York” }
    { “_id” : ObjectId(“4e398258c328b5026b86e540″), “age” : NumberLong(10) }
    { “_id” : ObjectId(“4e39835dc328b5026b86e541″), “age” : NumberLong(100) }
    { “_id” : ObjectId(“4e398382c328b5026b86e542″), “date” : ISODate(“2011-08-03T17:21:06.676Z”) }
    { “_id” : ObjectId(“4e3983fcc328b5026b86e543″), “bin” : BinData(2,”1234″) }

  • 相关阅读:
    我告诉你 电脑软件工具
    mysql 查询当天、本周,本月,上一个月的数据
    springboot:springboot+mybatis多数据源最简解决方案
    MySQL数据库优化的(经典必看)
    MyBatis 的强大特性之一便是它的动态 SQL之常用属性使用
    你知道Spring 中使用了哪些设计模式?
    kafka 相关面试问题
    掌握TCP/IP的通信原理(三次握手、四次挥手)。
    jsp和servlet实现文件的上传和下载
    Java获取数据库记录通过javabean映射,并存入list集合
  • 原文地址:https://www.cnblogs.com/cnsanshao/p/2806782.html
Copyright © 2011-2022 走看看