我有以下猫鼬模式
const analyticsSchema = new mongoose.Schema({
All_Rehires_Processed: Number,
All_Unique_Rehires_Processed: Number,
Files_Generated: Number,
Files_Generated_By_Position: {
Core: {
Server: {
Server0: Number,
Server1: Number,
Server2: Number,
Server3: Number
}
},
Nop: {
Server: {
Server0: Number,
Server1: Number,
Server2: Number,
Server3: Number
}
}
},
Rehires_By_Position: {
Core: {
Server: {
Server0: Number,
Server1: Number,
Server2: Number,
Server3: Number
}
},
Nop: {
Server: {
Server0: Number,
Server1: Number,
Server2: Number,
Server3: Number
}
}
}
});
当我检查数据库中的数据时,它将以正确的顺序返回结果。 我跑
db.getCollection('analytics').find({}).pretty()
它返回正确的顺序。
{
"_id": "5bdeafda975df6b565bfed2a",
"All_Rehires_Processed": 137,
"All_Unique_Rehires_Processed": 108,
"Files_Generated": 41,
"Files_Generated_By_Position": {
"Core": {
"Server": {
"Server0": 1,
"Server1": 1,
"Server2": 1,
"Server3": 1
}
},
"Nop": {
"Server": {
"Server0": 0,
"Server1": 1,
"Server2": 1,
"Server3": 1
}
}
},
"Rehires_By_Position": {
"Core": {
"Server": {
"Server0": 30,
"Server1": 30,
"Server2": 14,
"Server3": 11
}
},
"Nop": {
"Server": {
"Server0": 0,
"Server1": 1,
"Server2": 1,
"Server3": 1
}
}
},
"__v": 0
}
我当前正在将数据返回到明示端点
app.get('/analytics', (req, res) => {
Analytics.find({}, (err, analytics) => {
if (err) throw err;
// check if analytics
if (!analytics || analytics.length === 0) {
return res.status(404).json({
error: 'No files exist'
});
}
//analytics exist
return res.json(analytics);
});
});
这有效并以错误的顺序返回了我期望的数据。它按以下顺序返回数据
{
"Files_Generated_By_Position": {
"Core": {
"Server": {
"Server0": 1,
"Server1": 1,
"Server2": 1,
"Server3": 1
}
},
"Nop": {
"Server": {
"Server0": 0,
"Server1": 1,
"Server2": 1,
"Server3": 1
}
}
},
"Rehires_By_Position": {
"Core": {
"Server": {
"Server0": 30,
"Server1": 30,
"Server2": 14,
"Server3": 11
}
},
"Nop": {
"Server": {
"Server0": 0,
"Server1": 1,
"Server2": 1,
"Server3": 1
}
}
},
"_id": "5bdeafda975df6b565bfed2a",
"All_Rehires_Processed": 137,
"All_Unique_Rehires_Processed": 108,
"Files_Generated": 41,
"__v": 0
}
返回Express端点时,是否有任何方法可以强制json对象维持顺序?我需要对它进行排序还是我做错了什么?