我使用各種命令,find,findones,findone和updates等從mongodb中檢索文檔。其中大多數將返回保存的整個文檔,想。當找到或找到一個貓鼬允許使用人口。我試圖傳回傳遞給我的整個對象或查看整個對象,但數組出錯了。我仍然可以手動訪問這些值,但我想要整個對象的字符串等效。我試過JSON.stringify和util.inspect()。任何人都可以解決這個問題?我見過一些解決這個問題的舊帖子。如果問題是我如何存儲信息,我將發佈我的模型以及有關更改的有用建議。util.inspect/JSON.stringify在對象中的數組上不起作用
模型:第一產品,第二機器:
var productSchema = new Schema({
productName: String,
produtDesc: String,
author: String,
lastUpdate: { type: Date, default: Date.now },
minimum: { type: Schema.Types.ObjectId, ref: 'Machine' },
recommended: { type: Schema.Types.ObjectId, ref: 'Machine' }
});
var machineSchema = new Schema({
cpu: {
speed: String,
mfg: [String],
family: [String],
cores: Number,
architecture: String
},
memory: {
total: Number,
free: Number
},
storage: [ {
drive: String,
total: Number,
free: Number
} ],
os: {
family: String,
version: String,
uptime: Number
},
screen: {
height: Number,
width: Number
},
browser: {
family: String,
version: String
},
network: [ {
name: String,
speed: Number,
addresses: [ {
address: String,
type: String,
internal: Boolean
} ]
} ],
software: {
".net": [String],
java: [String],
acrobat: String
}
});
輸出代碼:
result.populate('recommended')
.populate('minimum')
.exec(function(err, done) {
console.log(util.inspect(done,
{ showHidden: false, depth: 10 }));
console.log(done.recommended.network[0]);
reply(util.inspect(done,
{ showHidden: false, depth: null }));
});
輸出樣本:第一util.inspect,第二JSON.stringify
// util.inspect
{ _id: 553dc1d60062ee501b88535d,
recommended:
{ _id: 553dc1d60062ee501b88535b,
__v: 0,
software: { acrobat: '9.0', java: [Object], '': [Object] },
network: [ [Object] ],
browser: { version: '9', family: 'Internet Explorer' },
screen: { width: 1920, height: 1080 },
os: { version: '6.1', family: 'Windows_NT' },
storage: [],
memory: { total: 3072 },
cpu: { cores: 4, speed: '2800', family: [Object], mfg: [Object] } },
minimum:
{ _id: 553dc1d60062ee501b885359,
__v: 0,
software: { acrobat: '9.0', java: [Object], '': [Object] },
network: [ [Object] ],
browser: { version: '9', family: 'Internet Explorer' },
screen: { width: 1024, height: 768 },
os: { version: '6.0', family: 'Windows_NT' },
storage: [],
memory: { total: 2048 },
cpu: { cores: 2, speed: '2666', family: [], mfg: [Object] } },
productName: 'Product Name',
author: 'chris.lohl',
__v: 0,
lastUpdate: Fri Apr 17 2015 07:01:37 GMT-0700 (US Mountain Standard Time)
}
// JSON.stringify
'{"_id":"553dc1d60062ee501b88535d","recommended":{"_id":"553dc1d60062ee501b88535b","__v":0,"software":{"acrobat":"9.0","java": ["1.6.0.24"],"":{"net":["2.0","3.5"]}},"network": [{"name":"ethernet","speed":1000,"_id":"553dc1d60062ee501b88535c","addresses":["[object Object]","[object Object]"]}],"browser": {"version":"9","family":"Internet Explorer"},"screen": {"width":1920,"height":1080},"os":{"version":"6.1","family":"Windows_NT"},"storage":[],"memory":{"total":3072},"cpu":{"cores":4,"speed":"2800","family":["i3","i5","i7"],"mfg":["intel"]}},"minimum":{"_id":"553dc1d60062ee501b885359","__v":0,"software":{"acrobat":"9.0","java":["1.6.0.2
4"],"":{"net":["2.0","3.5"]}},"network":[{"name":"ethernet","speed":1000,"_id":"
553dc1d60062ee501b88535a","addresses":["[object Object]","[object Object]"]}],"b
rowser":{"version":"9","family":"Internet Explorer"},"screen":{"width":1024,"hei
ght":768},"os":{"version":"6.0","family":"Windows_NT"},"storage":[],"memory":{"t
otal":2048},"cpu":{"cores":2,"speed":"2666","family":[],"mfg":["intel","amd"]}},
"productName":"Product Name","author":"chris.lohl","__v":0,"lastUpdate":"2015-04
-17T14:01:37.000Z"}'
的JSON提供的是從的console.log('JSON.stringify(完成))'?旁註:使用'done'作爲對象名稱有點奇怪,因爲它可能會讓以後認爲它是異步回調函數。 –
yes done是在這種情況下包含查詢傳遞的所有信息的對象。但util.inspect或stringify調用提供了兩個不同的輸出 – chacliff
您可能需要'JSON.stringify(obj,null,4)',其中4是要縮進的空格數。這是如此美妙,因爲它得到。 –