mongodb nodejs发现toarray不显示嵌套对象结果

时间:2018-06-26 16:36:08

标签: node.js mongodb toarray

所以我有一个简单的mongodb查询,该查询成功填充了结果,但未显示文档数组的结果。结果显示如下,您可以看到它显示了[对象]。请帮忙。

MongoClient.connect(atlas, { useNewUrlParser: true }, (err, db) => {
  if (err) throw err;

  var dbo = db.db(fdb);

  dbo.collection('seturoji').find({userID: 
'x0aJqI6q9SV8yHrwYvvMS'}).toArray( (err, r1) => {
if (err) throw err;

console.log(r1)


 })

})

[ { _id: 5b2ede6ee8d8e4be9bd7142d,
userID: 'x0aJqI6q9SV8yHrwYvvMS',
sID: '2O3KlZrTgw9zA3wxEWJieYCbFuZNrrQuEoPmPVm0BsIseDVZCt',
rootFiles: { '1': [Object], '2': [Object] } },
  { _id: 5b311367e8d8e4be9bee4e50,
userID: 'x0aJqI6q9SV8yHrwYvvMS',
sID: '2O3KlZrTgw9zbFuZNrrQuEoPmPVm0BsIseDVZCt',
rootFiles: [ [Object], [Object] ] } ]

1 个答案:

答案 0 :(得分:0)

这是使用console.log显示深层对象时的默认Node.js行为。您可以使用console.log选项,使用util.inspectdepth: null一样格式化它:

const util = require('util');
console.log(util.inspect(object, {depth: null}));

另一种选择是使用JSON.stringify对其进行序列化,尤其是采用某些格式,例如:

console.log(JSON.stringify(object, null, 4));

这将记录您的序列化对象,该对象缩进4个空格。

相关问题