在Nodejs中查看Mongodb

时间:2017-09-01 11:05:57

标签: angularjs node.js mongodb

我正在使用nodejs和MongoDb并使用MongoClient连接

我的ejs文件

<form method="POST" action="/addUser">
            <div class="modal-content">
            <div class="modal-header login-header">
            <button type="button" class="close" data-dismiss="modal">×</button>
            <h4 class="modal-title">Add Role</h4>
            </div>
            <div class="modal-body">
            <input type="hidden" name="_csrf"  value=<%=csrfToken%> />
            <input type="text" placeholder="Name" name="name" ng-model="name">                
            <input type="text" placeholder="Password" name="password" ng-model="password">                
            <select name="role" ng-model="role" >
            <option ng-repeat="role_type in roles"  value="{{role_type._id}}">{{role_type.role}}</option>
            </select>
            </div>
            <div class="modal-footer">
            <button type="submit" class="add-project">Save</button>
            <button type="submit" class="cancel" data-dismiss="modal">Close</button>
            </div>
            </div>
            </form>

并保存数据库中的值

app.post('/addUser', function(req, res) {
  var role = req.body.role;
  var uservalue = { "name" : req.body.name , "password" : req.body.password , "role" : ObjectId(role)}
  userCollection.save(uservalue,(err, result) => {
    if (err) return console.log(err)

    console.log('User Added');
    res.redirect('/users')
    console.log('result')

  })

});

我的Lookup看起来像

userCollection = db.collection('user');
userCollection.aggregate([
    { $lookup:
       {
         from: 'roles',
         localField: 'role',
         foreignField: '_id',
         as: 'permissiontable'
       }
     }
    ], function(err, res) {
    if (err) throw err;
    console.log(res);
  });

但我正在获得许可[对象]

如何解决这个问题我需要获取表值,而是获得对象单词

1 个答案:

答案 0 :(得分:0)

您需要将结果字符串化以将其打印出来。试试console.log(JSON.stringify(res))