
show dbs
use database_name
db.dropDatabase()
db.createCollection(name, options)
options:
举例:
db.createCollection("t_article", {
capped: false,
size: 5242880, //Specify a maximum size in bytes for a capped collection.
max:5000, //The maximum number of documents allowed in the capped collection.
})
show collections
db.<集合名>.drop()
insert() 或者 insertMany()
举例:
db.t_article.insertMany([
{
id: 1,
title: "2018新版java学习路线",
content: "java学习路线",
created: new Date("2018-10-10"),
modified: null,
categories: "默认分类",
tags: ["2018","Java",'学习路线'],
allow_comment: 1,
thumbnail: null,
hits: 92,
comments_num: 2,
t_comment:[
{
created:new Date("2018-12-13"),
ip:"0:0:0:0:0:0:0:1",
content:"很不错",
status:"approved",
author:"李四"
},
{
created:new Date("2018-12-14"),
ip:"0:0:0:0:0:0:0:1",
content:"很不错d",
status:"approved",
author:"张三"
}
]
},
{
id: 2,
title: "2018新版python学习路线",
content: "python学习路线",
created: new Date("2018-10-10"),
modified: null,
categories: "默认分类",
tags: ["2018","python",'学习路线'],
allow_comment: 1,
thumbnail: null,
hits: 18,
comments_num: 1,
t_comment:[
{
created:new Date("2018-12-13"),
ip:"0:0:0:0:0:0:0:1",
content:"很不错",
status:"approved",
author:"王五"
}
]
}
])
db.<集合名>.find(<query>,<projection>)
options:
举例:
// 1 查询:名字是tom 只显示username字段和email字段
db.t_user.find({username:"tom"},{username: 1,email: 1})
// 返回
{
"_id" : ObjectId("642c1d48316b1ef08b947f36"),
"username" : "tom",
"email" : "[email protected]"
}
//2 查询点击量和评论数都大于等于1的文章,查询结果只显示文章标题和tags
db.t_article.find(
{
hits: {$gt:1}, // $gt大于 $lt小于 $gte大于等于 $lte小于等于
comments_num: {$gt:1},
},
{
title: 1,
tags: 1
}
)
// 3 查询文章标题以:2018开头的文章,查询结果除了文章内容不显示,其他都显示。
db.t_article.find(
{
title: {$regex:'^2018.*'},
},
{
content: 0,
}
)
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)
options
举例:
// 将用户 tom 的权限更新为ROLE_admin
db.t_user.update(
{username: "tom"},
{$set:{"authority":"ROLE_admin"}},
{ multi: false, upsert: true}
)
db.colection.remove(<query>,<justOne>)
options: