• koa学习中的一系列问题-mongodb


    Mongoose是MongoDB的一个对象模型工具,基于node-mongodb-native开发。

    数据库连接

    mongoose.connect('mongodb://user:pass@localhost:port/database');
    
    // replica sets
    var uri = 'mongodb://user:pass@localhost:port,anotherhost:port,yetanother:port/mydatabase';
    mongoose.connect(uri);
    
    // with options
    mongoose.connect(uri, options);
    
    // connecting to multiple mongos
    var uri = 'mongodb://hostA:27501,hostB:27501';
    var opts = { mongos: true };
    mongoose.connect(uri, opts);
    
    // optional callback that gets fired when initial connection completed
    var uri = 'mongodb://nonexistent.domain:27000';
    mongoose.connect(uri, function(error) {
      // if error is truthy, the initial connection failed.
    })

    1、mongoose中的 find 和 findOne 区别?

    定义:db.collection.findOne(queryprojection)

     find 和 findOne都是用来查找指定表的数据的;

    find指的是查找指定表的所有数据,返回的是数组

    User.find().then((result)=>{
                 console.log(result)   //返回一个数组
    })

    findOne指的是查找指定表的单条数据,返回一个对象

    User.findOne({name:"huang"}).then((result)=>{
                 console.log(result);  //返回一个对象
     })
    

     获取 “huang” 可以用 result.name

    2、mongodb的schema和model的理解

      不同于关系型数据库,MongoDB作为文档型数据库,Scheme、model、collection、document是其中的四大元素。document是MongoDB里的基本存储单位,collection是众多同类document的集合。Schema定义了一类document的模板,让这一类document在数据库中有一个具体的构成、存储模式。而Schema仅仅是定义了Document是什么样子的,至于生成document和对document进行各种操作(增删改查)则是通过相对应的model来进行的。

      需要说明的是MongoDB中实际上只有collection和document,Schema和model不过是定义和生成前二者过程中的工具而已。

    var mongoose = require('mongoose');
    mongoose.connect('mongodb://localhost/test');
    var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'connection error:'));
    db.once('open', function() {
        console.log('connected');
    });
    var Schema = mongoose.Schema;        //定义schema,定义同一个collection的document模板
    var blogSchema = Schema
    ({
        blogTitle : String,
        body : String,
        comments : [{body : String, date : Date}],
        hidden : Boolean,
        state : {
            favors : Number,
            marks : Number
        }
    });
    blogSchema.add({reprints : Number});
    blogSchema.index({blogTitel : 1, reprints : -1});
    var Blog = mongoose.model('Blog', blogSchema);    //编译model
    var blog01 = new Blog({                  //生成documen数据
        blogTitle : 'The total eclipse of hreat',
        body : '........',
        comments : [{body : "That's awesome", date : new Date()}],
        hidden : false,
        state : {
            favors : 999,
            marks : 999
        },
        reprints : 999
    });
    blog01.save(function(err){
        if (err){
            return console.error(err);
        }
        console.log('saved');
    });
    
    mongoose.connection.close();

    其他

    Date.now() 方法返回自 1970 年 1 月 1 日 00:00:00 (UTC) 到当前时间的毫秒数。

  • 相关阅读:
    Day5.打包解压,分区,软链接和硬链接
    Day4.权限命令
    Day3.linux基础命令
    macos proxy_bypass_macosx_sysconf exception
    网站收集
    django test
    django test, app aren't loaded yet
    sqlalchemy insert on duplicate update
    linux随机字符串
    Celery Received unregistered task of type
  • 原文地址:https://www.cnblogs.com/shary-blue/p/14342514.html
Copyright © 2020-2023  润新知