// 引入 mongoose 第三方模块 用来操作数据库
const mongoose = require('mongoose');
// 数据库连接
mongoose.connect('mongodb://localhost/playground', {
useNewUrlParser: true,
useUnifiedTopology: true
})
// 连接成功
.then(() => console.log('数据库连接成功...'))
// 连接失败
.catch(err => console.log(err, '数据库连接失败...'));
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true
}
});
const postSchema = new mongoose.Schema({
title: {
type: String
},
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
});
// 用户集合
const User = mongoose.model('User', userSchema);
// 文章集合
const Post = mongoose.model('Post', postSchema);
// 创建用户
// User.create({
// name: 'item'
// }).then(result => console.log(result))
// 创建文章
// Post.create({
// title: '1234566',
// author: '5ee0e0ef3d9a1c0be82fd250'
// }).then(result => console.log(result))
// 查询
// Post.find()
// .populate('author')
// .then(result => console.log(result));