zoukankan      html  css  js  c++  java
  • 关联查询

    // 引入 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));
  • 相关阅读:
    0X01 OWASP WebGoat Splitting
    subprocess
    Python中getopt()函数的使用
    Python3_UDP客户端
    Python3编写TCP客户端
    Python3---pymysql库____操作数据库
    review——database (1)CH8-Relational Database Design
    删除的review——数据库 (1)CH6关系数据理论
    review——C# (15)转换
    review——C# (14)接口
  • 原文地址:https://www.cnblogs.com/ericblog1992/p/13089299.html
Copyright © 2011-2022 走看看