zoukankan      html  css  js  c++  java
  • MongoDB 集合关联

    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))
    
  • 相关阅读:
    SQL 联合语句
    Strust2MVC
    struts2执行流程
    struts2的验证框架
    poj 1201 Intervals
    FZOJ Problem 2219 StarCraft
    poj 3470 Walls
    hdu 6021 MG loves string
    FZOJ Problem 2150 Fire Game
    FZOJ Problem 2148 Moon Game
  • 原文地址:https://www.cnblogs.com/liea/p/12129229.html
Copyright © 2011-2022 走看看