mongodb命令基础知识点

本文详细介绍了MongoDB的基础操作,包括启动mongo、进入特定数据库、显示数据库、数据表的插入、删除、查询、更新等操作,以及数据过滤、排序、计数等高级用法,帮助读者掌握MongoDB的基本命令。
摘要由CSDN通过智能技术生成
Microsoft Windows [版本 10.0.17134.950]
(c) 2018 Microsoft Corporation。保留所有权利。

C:\Users\天主极乐大帝>d:

1、进入bin目录

D:\>cd  D:\MongoDB\Server\3.4\bin

D:\MongoDB\Server\3.4\bin>mongod  --config  "D:\MongoDB\Server\3.4\mongo.config"  --install --serviceName "MongoDB"
2019-09-04T11:30:58.433+0800 I CONTROL  [main] log file "D:\MongoDB\Server\3.4\data\log\mongo.log" exists; moved to "D:\MongoDB\Server\3.4\data\log\mongo.log.2019-09-04T03-30-58".

2、命令行启动mongo

D:\MongoDB\Server\3.4\bin>mongo
MongoDB shell version v3.4.16-41-g83c3022fe4
connecting to: mongodb://127.0.0.1:27017
2019-09-04T11:33:05.665+0800 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017 after 5000ms milliseconds, giving up.
2019-09-04T11:33:05.665+0800 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:240:13
@(connect):1:6
exception: connect failed

D:\MongoDB\Server\3.4\bin>mongo
MongoDB shell version v3.4.16-41-g83c3022fe4
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.16-41-g83c3022fe4
Server has startup warnings:
2019-09-03T20:34:03.839-0700 I CONTROL  [initandlisten]
2019-09-03T20:34:03.839-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-09-03T20:34:03.840-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-09-03T20:34:03.840-0700 I CONTROL  [initandlisten]

3、进入tzl数据库
> use tzl switched to db tzl
4、显示数据库

> show  dbs
admin      0.000GB
local      0.000GB
lys        0.000GB
newtestdb  0.000GB
test       0.000GB
> use  tzl
switched to db tzl

5、向tzl数据表中插入一条数据

> db.tzl.insert({'name':'liyisong'})
WriteResult({ "nInserted" : 1 })
> show  dbs
admin      0.000GB
local      0.000GB
lys        0.000GB
newtestdb  0.000GB
test       0.000GB
tzl        0.000GB
> use  tzl
switched to db tzl

6、删除该数据库

> db.dropDatabase()
{ "dropped" : "tzl", "ok" : 1 }
> show  dbs
admin      0.000GB
local      0.000GB
lys        0.000GB
newtestdb  0.000GB
test       0.000GB
> use   lll
switched to db lll
> show  dbs
admin      0.000GB
local      0.000GB
lys        0.000GB
newtestdb  0.000GB
test       0.000GB
> use  student
switched to db student
> use  taoche
switched to db taoche
> use  student
switched to db student
> gb
2019-09-04T14:23:14.076+0800 E QUERY    [thread1] ReferenceError: gb is not defined :
@(shell):1:1

7、db显示在哪个数据库

   > db
    student
    > db.lys.insert({'name:'liyisong'})
    2019-09-04T14:24:14.901+0800 E QUERY    [thread1] SyntaxError: missing : after property id @(shell):1:22
    > db.lys.insert({'name:'liyisong'})
    2019-09-04T14:24:55.152+0800 E QUERY    [thread1] SyntaxError: missing : after property id @(shell):1:22
    > use  student
    switched to db student
    > db.lys.insert({'name':'liyisong'})
    WriteResult({ "nInserted" : 1 })

8、show collections查询数据表

> show  collections
lys
> db.lys.insert({'name':'tzl'})
WriteResult({ "nInserted" : 1 })
> db.lys.insert({'name':'tzl'})
WriteResult({ "nInserted" : 1 })

9、db.数据表.drop()删除该数据表

> db.lys.drop()
true
> show  collections
> db.lys.insert({'name':'lys','age':18,'sex':1})
WriteResult({ "nInserted" : 1 })
> db.lys.insert({'name':'wtt','age':18,'sex':1})
WriteResult({ "nInserted" : 1 })
> db.lys.insert({'name':'ljj','age':18,'sex':1})
WriteResult({ "nInserted" : 1 })
> db.lys.insert({'name':'czh','age':18,'sex':1})
WriteResult({ "nInserted" : 1 })
> db.lys.insert({'name':'czh','age':19,'sex':1})
WriteResult({ "nInserted" : 1 })
> db.lys.insert({'name':'wtt','age':17,'sex':1})
WriteResult({ "nInserted" : 1 })
> db.lys.insert({'name':'syj','age':17,'sex':2})
WriteResult({ "nInserted" : 1 })
> db.lys.insert({'name':'dss','age':18,'sex':2})
WriteResult({ "nInserted" : 1 })

10、db.数据表.find()查找该数据表内所有内容

> db.lys.find()
{ "_id" : ObjectId("5d6f5b03ae8dbac5ec948bbb"), "name" : "lys", "age" : 18, "sex" : 1 }
{ "_id" : ObjectId("5d6f5b14ae8dbac5ec948bbc"), "name" : "wtt", "age" : 18, "sex" : 1 }
{ "_id" : ObjectId("5d6f5b1cae8dbac5ec948bbd"), "name" : "ljj", "age" : 18, "sex" : 1 }
{ "_id" : ObjectId("5d6f5b23ae8dbac5ec948bbe"), "name" : "czh", "age" : 18, "sex" 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值