Mongoose,子文档是对象而不是 json [英] Mongoose, sub document is objects and not json
问题描述
我正在使用 mongoose 查询我的 mongodb,但我不明白为什么返回的子文档只是 Object 类型,而不是 JSON.
I am querying my mongodb using mongoose, but i don't understand why the returned sub docs are just of type Object, instead of JSON.
使用
hero.find({} ,{'deck' : {$elemMatch:{name:'Guard Tower'}}}, function(err, tower) {
console.log(tower);
}
返回
[ { _id: 507ac406ba6ecb1316000001,
deck:
[ { name: 'Guard Tower',
description: 'This tower shoots stuff.',
cost: 13,
sellValue: 7,
radius: 180,
speed: 40,
dmg_min: 0,
dmg_max: 0,
actual_height: 40,
sprite: 'guardtower_red.png',
anim: [Object],
size: [Object],
projectile: [Object],
upgrade: [Object] } ] } ]
动画、大小、弹丸、升级等子文档,是对象,需要嵌套信息,如何获取信息?不进行其他查询?
Subdocument like anim, size, projectile, upgrade, is Object, i need the information nested, how can i get the information? Without making another query?
推荐答案
所有的文档和子文档都是 JavaScript 中的对象.只是 console.log
在调用 util.inspect
时使用默认深度 2 来格式化您的文档以进行输出.您可以通过调用 util.inspect
自己:
The all docs and subdocs are objects in JavaScript. It's just that console.log
uses the default depth of 2 when calling util.inspect
to format your document for output. You can output all levels of the document by calling util.inspect
yourself:
var util = require('util');
hero.find({} ,{'deck' : {$elemMatch:{name:'Guard Tower'}}}, function(err, tower) {
console.log(util.inspect(tower, false, null));
});
这篇关于Mongoose,子文档是对象而不是 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!