MongoDB-2-命令

版权声明:本文为博主原创文章,未经博主允许不得转载

MongoDB-2-命令

show dbs;–查看所有库

use 库名;–创建库,没有该库就创建库,有就切换为该库

db;–查看当前使用的库

db.dropDatabase();–删除当前库

db.createCollection(name, options);–创建集合
name: 要创建的集合名称
options: 可选参数, 指定有关内存大小及索引的选项
参数说明:
options 可以是如下参数:
在这里插入图片描述
show collections;–查看都有哪些集合
show tables;–查看都有哪些集合
db.getCollectionInfos();–查看都有哪些集合

db.集合名.drop();–删除指定集合

db.集合名.insert(document);–向集合中插入文档,文档需要是json格式,如db.user.insert({“name”:“Gqc”});

db.集合名.find(query);–条件查询
db.集合名.find(query).pretty();–格式化查询
db.集合名.find(query).count();–查询有多少条数据

db.集合名.update(
,
,
{
upsert: ,
multi:
}
)
参数说明:
query : update的查询条件,类似sql update查询内where后面的。
update : update的对象和一些更新的操作符(如 , , set…)等,也可以理解为sql update查询内set后面的
upsert : 可选,这个参数的意思是,如果不存在update的记录,是否插入该数据,true为插入,默认是false,不插入。
multi : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
例:
db.student.update({“name”:“gqc”},{“age”:“18”});–将名为gqc的第一条数据年龄改为18,注意:原有数据中其他列的值都会改为空
db.student.update({“name”:“gqc”},{KaTeX parse error: Expected 'EOF', got '}' at position 17: …et:{"age":"18"}}̲);--将名为gqc的第一条数…set:{“age”:“18”}},{upsert:true});–更改名为gqc的数据,如果没有则新增
db.student.update({“name”:“gqc”},{$set:{“age”:“18”}},{multi:true});–将名为gqc的所有数据都更改

db.集合名.remove(
,

)
参数说明:
query :(可选)删除的文档的条件。
justOne : (可选)如果设为 true 或 1,则只删除一个文档。
db.集合名.remove({});–删除所有数据

MongoDB 与 RDBMS Where 语句比较

如果你熟悉常规的 SQL 数据,通过下表可以更好的理解 MongoDB 的条件语句查询:
在这里插入图片描述
db.集合名.find({key1:value1, key2:value2}).pretty();–多个条件的And查询
例:db.user.find({“name”:“gqc”,“age”:18});–类似于where name=‘gqc’ and age=18
在这里插入图片描述

and和or连用,例:
db.col.find({“age”: {$gt:20}, $or: [{“name”: “xk”},{“sex”: 1}]}).pretty();–类似于where age>20 and(name=‘xk’ or sex=1)

db.集合名.find().skip(开始坐标).limit(显示条数);–分页查询,开始坐标的公式应为(页数-1)*条数

db.集合名.find().sort({排序列:排序方式});–将集合排序,排序方式1为升序,-1为降序

日期使用:
new Date();–当前时间
ISODate();–当前时间
注:new Date();和ISODate();获得的当前时间是格林尼治时间,与我们相差8小时
Date();–获得当前时间

下面是我敲的一些命令

MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.6
Server has startup warnings:
2019-04-10T13:38:45.522+0800 I CONTROL  [initandlisten]
2019-04-10T13:38:45.523+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-04-10T13:38:45.523+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-04-10T13:38:45.523+0800 I CONTROL  [initandlisten]
> show dbs
admin  0.000GB
local  0.000GB
> show dbs
admin  0.000GB
local  0.000GB
> use mr
switched to db mr
> db;
mr
> use admin
switched to db admin
> db;
admin
> use mr
switched to db mr
> db;
mr
> db.dropDatabase();
{ "ok" : 1 }
> db
mr
> show dbs;
admin  0.000GB
local  0.000GB
> use admin;
switched to db admin
> db;
admin
> db.createCollection("user";)
2019-04-10T13:55:08.121+0800 E QUERY    [thread1] SyntaxError: missing ) after argument list @(shell):1:26
> db.createCollection("user");
{ "ok" : 1 }
> show dbs;
admin  0.000GB
local  0.000GB
> use mr;
switched to db mr
> dbs;
2019-04-10T13:56:39.910+0800 E QUERY    [thread1] ReferenceError: dbs is not defined :
@(shell):1:1
> db;
mr
> db.createCollection("user");
{ "ok" : 1 }
> show dbs;
admin  0.000GB
local  0.000GB
mr     0.000GB
> show collections;
user
> show tables;
user
> db.getCollectionInfos();
[
        {
                "name" : "user",
                "type" : "collection",
                "options" : {

                },
                "info" : {
                        "readOnly" : false
                },
                "idIndex" : {
                        "v" : 2,
                        "key" : {
                                "_id" : 1
                        },
                        "name" : "_id_",
                        "ns" : "mr.user"
                }
        }
]
> db.user.insert({"name":"yuanbo"});
WriteResult({ "nInserted" : 1 })
> db.getCollectionInfos();
[
        {
                "name" : "user",
                "type" : "collection",
                "options" : {

                },
                "info" : {
                        "readOnly" : false
                },
                "idIndex" : {
                        "v" : 2,
                        "key" : {
                                "_id" : 1
                        },
                        "name" : "_id_",
                        "ns" : "mr.user"
                }
        }
]
> db.createCollection("stu",<max:2000>);
2019-04-10T14:07:08.694+0800 E QUERY    [thread1] SyntaxError: expected expression, got '<' @(shell):1:26
> db.createCollection("stu",(max:2000));
2019-04-10T14:07:38.645+0800 E QUERY    [thread1] SyntaxError: missing ) in parenthetical @(shell):1:30
> db.createCollection("stu",(max:2000))
2019-04-10T14:09:18.940+0800 E QUERY    [thread1] SyntaxError: missing ) in parenthetical @(shell):1:30
> db.createCollection("stu",(max:2000))
2019-04-10T14:09:47.881+0800 E QUERY    [thread1] SyntaxError: missing ) in parenthetical @(shell):1:30
> show dbs;
admin  0.000GB
local  0.000GB
mr     0.000GB
> use user;
switched to db user
> db;
user
> show collections;
> show collections;
> db.createCollections("student",(max:1000))
2019-04-10T14:13:34.008+0800 E QUERY    [thread1] SyntaxError: missing ) in parenthetical @(shell):1:35
> show tables;
> show dbs;
admin  0.000GB
local  0.000GB
mr     0.000GB
> use mr;
switched to db mr
> db;
mr
> db.createCollection("stu");
{ "ok" : 1 }
> show collections;
stu
user
> show tables;
stu
user
> db.user.drop();
true
> show collections;
stu
> db.stu.drop();
true
> show tables;
> db.user.insert({"name":"yuanbo"})
WriteResult({ "nInserted" : 1 })
> db.user.insert((name:"gqc",age:19))
2019-04-10T14:23:42.793+0800 E QUERY    [thread1] SyntaxError: missing ) in parenthetical @(shell):1:20
> db.user.insert((name:"gqc",age:19));
2019-04-10T14:23:47.288+0800 E QUERY    [thread1] SyntaxError: missing ) in parenthetical @(shell):1:20
> db.user.find()
{ "_id" : ObjectId("5cad8b9a291f5801b1f2040c"), "name" : "yuanbo" }
> db.user.insert((name:"gqc",age:19));
2019-04-10T14:25:51.100+0800 E QUERY    [thread1] SyntaxError: missing ) in parenthetical @(shell):1:20
> db.user.insert(("name":"gqc","age":19));
2019-04-10T14:28:23.123+0800 E QUERY    [thread1] SyntaxError: missing ) in parenthetical @(shell):1:22
> db.user.insert(("name":"gqc","age":19))
2019-04-10T14:28:34.136+0800 E QUERY    [thread1] SyntaxError: missing ) in parenthetical @(shell):1:22
> db.user.insert({"name":"gqc","age":19})
WriteResult({ "nInserted" : 1 })
> db.user.find()
{ "_id" : ObjectId("5cad8b9a291f5801b1f2040c"), "name" : "yuanbo" }
{ "_id" : ObjectId("5cad8d4a291f5801b1f2040d"), "name" : "gqc", "age" : 19 }
> show tables
user
> db.user.insert({"name":"yuanbo1","age":"19","sex":"男"})
WriteResult({ "nInserted" : 1 })
> db.user.find()
{ "_id" : ObjectId("5cad8b9a291f5801b1f2040c"), "name" : "yuanbo" }
{ "_id" : ObjectId("5cad8d4a291f5801b1f2040d"), "name" : "gqc", "age" : 19 }
{ "_id" : ObjectId("5cad8de2291f5801b1f2040e"), "name" : "yuanbo1", "age" : "19", "sex" : "男" }
> db.user.find({"name":"yuanbo"})
{ "_id" : ObjectId("5cad8b9a291f5801b1f2040c"), "name" : "yuanbo" }
> db.user.find().pretty()
{ "_id" : ObjectId("5cad8b9a291f5801b1f2040c"), "name" : "yuanbo" }
{ "_id" : ObjectId("5cad8d4a291f5801b1f2040d"), "name" : "gqc", "age" : 19 }
{
        "_id" : ObjectId("5cad8de2291f5801b1f2040e"),
        "name" : "yuanbo1",
        "age" : "19",
        "sex" : "男"
}
> db.user.find().count()
3
> db.user.update({"name":"gqc"},{"name":"gqc1"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find();
{ "_id" : ObjectId("5cad8b9a291f5801b1f2040c"), "name" : "yuanbo" }
{ "_id" : ObjectId("5cad8d4a291f5801b1f2040d"), "name" : "gqc1" }
{ "_id" : ObjectId("5cad8de2291f5801b1f2040e"), "name" : "yuanbo1", "age" : "19", "sex" : "男" }
> db.user.update({"name":"gqc1"},{"name":"gqc","age":"35"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : ObjectId("5cad8b9a291f5801b1f2040c"), "name" : "yuanbo" }
{ "_id" : ObjectId("5cad8d4a291f5801b1f2040d"), "name" : "gqc", "age" : "35" }
{ "_id" : ObjectId("5cad8de2291f5801b1f2040e"), "name" : "yuanbo1", "age" : "19", "sex" : "男" }
> db.user.update({"name":"gqc"},{$set:{"age":"30"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : ObjectId("5cad8b9a291f5801b1f2040c"), "name" : "yuanbo" }
{ "_id" : ObjectId("5cad8d4a291f5801b1f2040d"), "name" : "gqc", "age" : "30" }
{ "_id" : ObjectId("5cad8de2291f5801b1f2040e"), "name" : "yuanbo1", "age" : "19", "sex" : "男" }
> db.user.update({"name":"gqc1"},{$set:{"name":"gqc3"}},{upset:true})
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
> db.user.find()
{ "_id" : ObjectId("5cad8b9a291f5801b1f2040c"), "name" : "yuanbo" }
{ "_id" : ObjectId("5cad8d4a291f5801b1f2040d"), "name" : "gqc", "age" : "30" }
{ "_id" : ObjectId("5cad8de2291f5801b1f2040e"), "name" : "yuanbo1", "age" : "19", "sex" : "男" }
> db.user.update({"name":"gqc1"},{$set:{"name":"gqc3"}},{upsert:true})
WriteResult({
        "nMatched" : 0,
        "nUpserted" : 1,
        "nModified" : 0,
        "_id" : ObjectId("5cad95749022790e68c02af1")
})
> db.user.find()
{ "_id" : ObjectId("5cad8b9a291f5801b1f2040c"), "name" : "yuanbo" }
{ "_id" : ObjectId("5cad8d4a291f5801b1f2040d"), "name" : "gqc", "age" : "30" }
{ "_id" : ObjectId("5cad8de2291f5801b1f2040e"), "name" : "yuanbo1", "age" : "19", "sex" : "男" }
{ "_id" : ObjectId("5cad95749022790e68c02af1"), "name" : "gqc3" }
> db.user.update({"name":"gqc3"},{$set:{"name":"gqc4"}},{multi:true})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find()
{ "_id" : ObjectId("5cad8b9a291f5801b1f2040c"), "name" : "yuanbo" }
{ "_id" : ObjectId("5cad8d4a291f5801b1f2040d"), "name" : "gqc", "age" : "30" }
{ "_id" : ObjectId("5cad8de2291f5801b1f2040e"), "name" : "yuanbo1", "age" : "19", "sex" : "男" }
{ "_id" : ObjectId("5cad95749022790e68c02af1"), "name" : "gqc4" }
> db.user.remove({"name":"gqc4"},{justOne})
2019-04-10T15:11:02.636+0800 E QUERY    [thread1] ReferenceError: justOne is not defined :
@(shell):1:33
> db.user.remove({"name":"gqc4"},{justOne:true})
WriteResult({ "nRemoved" : 1 })
> db.user.find()
{ "_id" : ObjectId("5cad8b9a291f5801b1f2040c"), "name" : "yuanbo" }
{ "_id" : ObjectId("5cad8d4a291f5801b1f2040d"), "name" : "gqc", "age" : "30" }
{ "_id" : ObjectId("5cad8de2291f5801b1f2040e"), "name" : "yuanbo1", "age" : "19", "sex" : "男" }
> db.user.find({age:{$lte:19}})
> db.user.find({"age":{$lte:19}})
> db.user.find({"age":{$lte:"19"}})
{ "_id" : ObjectId("5cad8de2291f5801b1f2040e"), "name" : "yuanbo1", "age" : "19", "sex" : "男" }
> db.user.find({"name":"yuanbo1","age":"19"})
{ "_id" : ObjectId("5cad8de2291f5801b1f2040e"), "name" : "yuanbo1", "age" : "19", "sex" : "男" }
> db.user.find({$or:[{"name":"yuanbo1"},{"name":"gqc"}]})
{ "_id" : ObjectId("5cad8d4a291f5801b1f2040d"), "name" : "gqc", "age" : "30" }
{ "_id" : ObjectId("5cad8de2291f5801b1f2040e"), "name" : "yuanbo1", "age" : "19", "sex" : "男" }
> db.user.find({"age": {$gt:20}, $or: [{"name": "yuanbo"},{"sex":"男"}]}).pretty();
> db.user.find({"age": {$gt:16}, $or: [{"name": "yuanbo"},{"sex":"男"}]}).pretty();
> db.user.find({"age": {$gt:16}, $or: [{"name": "yuanbo"},{"sex":"男"}]}).pretty();
> db.user.find({"age": {$gt:"6"}, $or: [{"name": "yuanbo"},{"sex":"男"}]}).pretty())
> db.user.find({"age": {$gt:"6"}, $or: [{"name": "yuanbo"},{"sex":"男"}]}).pretty()
> db.user.find({"age": {$gt:"6"}, $or: [{"name": "yuanbo1"},{"sex":"男"}]}).pretty()
> db.user.find({"age": {$gt:"6"}, $or: [{"name": "yuanbo"},{"sex":"男"}]}).pretty())
> new Date()
ISODate("2019-04-10T07:36:33.912Z")
> ISODate()
ISODate("2019-04-10T07:36:49.396Z")
> Date()
Wed Apr 10 2019 15:36:59 GMT+0800
> db.user.remove({"name":"gqc4"},{justOne:true})

猜你喜欢

转载自blog.csdn.net/weixin_44914784/article/details/89312671