小编nic*_*fly的帖子

如何在node.js中重用mongodb连接

我正在使用mongodb的node-mongodb-native驱动程序来编写一个网站.

我有一个关于如何打开mongodb连接一次的问题,然后在集合名称用户user.js和集合名称帖子中使用它comment.js

我想在db.js那时打开数据库连接,为用户和帖子集合插入/保存数据

目前代码,我的 db.js

var Db = require('mongodb').Db,
    Connection = require('mongodb').Connection,
    Server = require('mongodb').Server;
module.exports = new Db(
    'blog', 
    new Server('localhost', Connection.DEFAULT_PORT, {auto_reconnect: true})
);
Run Code Online (Sandbox Code Playgroud)

我曾经db.jsuser.js如下

var mongodb = require('./db');

function User(user){
  this.name = user.name;
  this.password = user.password;
  this.email = user.email;
};

module.exports = User;

User.prototype.save = function(callback) {//save user information
  //document to save in db
  var user = {
      name: this.name,
      password: this.password,
      email: this.email
  };
  mongodb.close();
  //open mongodb …
Run Code Online (Sandbox Code Playgroud)

node.js node-mongodb-native

6
推荐指数
1
解决办法
6364
查看次数

shell中的$# -eq是什么意思

我是 shell 脚本的新手,在下面的 shell 脚本中

#!/usr/bin/env sh
PREFIX=${PREFIX-/usr/local}
VERSIONS_DIR=$PREFIX/n/versions

test -d $VERSIONS_DIR || mkdir -p $VERSIONS_DIR

if test $# -eq 0; then
Run Code Online (Sandbox Code Playgroud)

$# -eq 0这个shell脚本中的意思是什么?

shell

2
推荐指数
1
解决办法
2848
查看次数

标签 统计

node-mongodb-native ×1

node.js ×1

shell ×1