标签:str connect cat 成功 hang find cti sch mongod
const mongoose = require('mongoose')
mongoose.connect('mongodb://164.red/test', { useUnifiedTopology: true })
.then(res => console.log('数据库连接成功'))
.catch(res => console.log('数据库连接失败'))
// 用户集合规则
const userSchema = new mongoose.Schema({ name: String})
// 文章集合规则
const postSchema = new mongoose.Schema({
title: String,
// 关联集合
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
})
// 用户集合
const User = mongoose.model('User', userSchema )
// 文章集合
const Post = mongoose.model('Post', postSchema)
User.create({name: 'zhangsan'}).then(res => console.log(res))
Post.create({title: 'fsdfs', author: '5e0c4c2a237809c3ae003992'}).then(res => console.log(res))
Post.find().populate('author').then(res=> console.log(res))
标签:str connect cat 成功 hang find cti sch mongod
原文地址:https://www.cnblogs.com/liea/p/12129229.html