# 1.mongodb 记录
- 启动数据库:
mongod --config filepath
win:mongod --config D:\MongoDB\etc\mongodb.conf
mac:mongod -f /usr/local/mongodb/etc/mongodb.conf
robo 3T可视化工具mongo命令启动shell查看数据库ps aux | grep mongodb查找服务kill -9 7337杀进程服务器
mongodb(https://www.jianshu.com/p/f8b0d088a032)部署创建表
db.createCollection('name')导入数据,导入后需要在
bin新建cmd窗口才能查看mongoimport -d db_name -c collection_name --file file_path读取表数据的时候,修改给给字段赋值,必须先在
schema里面定义好这些字段先
显示数据库
show dbs显示表
show collections建立索引
db.person.ensureIndex({"name":1},{"unique":true}),1代表升序,-1代表降序,添加了unique标志后name如果有重复的键值就不能插入
4.时间戳保存格式写法:
let commentSchema = new Schema({
userid: {
type: mongoose.Schema.ObjectId,
ref: 'User'
},
username:String,
content: String,
createtime: {
type: String,
default: Date.now
},
updatetime:{
type:String,
default:Date.now
}
})
commentSchema.pre('save',function (next){
if (this.isNew) {
this.createtime = this.updatetime = Date.now()
} else {
this.updatetime = Date.now()
}
})
// save findOneAndDelete返回被操作文档(doc)本身