在express node.js中控制台上的mongoose打印值

时间:2015-11-25 05:45:31

标签: node.js express mongoose

我从mongoose到查询

获得价值
          console.log(docs);
output :  [ { usercount: 0, _id: 5653fab1d6b5f1cf624b97fd },
         { usercount: 1, _id: 56541b49d6b5f1cf624b97ff } ]

       console.log(docs[1]._id);
output:   56541b49d6b5f1cf624b97ff

      console.log(docs[1].usercount);
output: undefined

那么,我将如何获得usercount的值。

我的模型uservisitcount.js包含以下代码:

var mongoose = require('mongoose')
  ,Schema = mongoose.Schema
  uservisitcountSchema = new Schema( {
      usercount:String,
  }),
Usercount = mongoose.model('uservisitcount', uservisitcountSchema);

module.exports = Usercount;

将此模型包含在控制器中:

var Uservisitcount = require('../models/uservisitcount');

我跑了下面的查询:

Uservisitcount.find({},function(err,docs){
       console.log(docs);
       console.log(docs[1]._id);
       console.log(docs[1].usercount);
   }

1 个答案:

答案 0 :(得分:1)

对返回的数据应用JSON.stringify()

Uservisitcount.find({},function(err,docs){
  if (err) throw err;
  console.log(JSON.stringify(docs)); 
  // or: 
  console.log('%s', docs); 
}