• 关联查询


    // 引入 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));
  • 相关阅读:
    线程、同步
    异常、线程
    Map
    List、Set
    Collection、泛型
    Object类、常用API
    h5-上传图片预览
    js 获取get参数
    《转》完美解决微信video视频隐藏控件和内联播放问题
    写入文件(覆盖文件的内容)
  • 原文地址:https://www.cnblogs.com/ericblog1992/p/13089299.html
Copyright © 2020-2023  润新知