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));
  • 相关阅读:
    菜鸟小结
    计算几何题目整理(转)
    poj 3299 Humidex
    基于C的文件操作(转)
    poj 1328 Radar Installation
    poj 1321 棋盘问题(dfs)
    poj 3302 Subsequence
    C# 资产(Property) 与普通字段(field)变量的区别
    Jumping into Cloud, Be Sure You Know How to Get Out
    关于语言的想法。
  • 原文地址:https://www.cnblogs.com/ericblog1992/p/13089299.html
Copyright © 2011-2022 走看看