我正在使用MEAN堆栈在网格中显示以下数组。
嵌套数组:
{
"_id":"1",
"appDomain":[
{
"appDomainName":"XYZ",
"dashboard":[
{
"envName":"UAT",
"envDetails":[
{
"hostnme":"ABC",
"ip":"sdsdsdsd",
"cpu":"-------",
"memory":"-------",
"disk":"-------",
"downtime":"sdsdsdsd",
"version":"dsdsdsd",
"hostDetails":[
{
"hostHdrName":"tomcat",
"hostDetails":[
{
"hostname":"FLT",
"status":"UP",
"path":"dfdf",
"host":"sdsdsd",
"port":"1112"
}
]
}
]
}
]
}
]
},
{
"appDomainName":"ABC",
"dashboard":[
{
"envName":"UAT",
"envDetails":[
{
"hostnme":"ABC",
"ip":"sdsdsdsd",
"cpu":"-------",
"memory":"-------",
"disk":"-------",
"downtime":"sdsdsdsd",
"version":"dsdsdsd",
"hostDetails":[
{
"hostHdrName":"tomcat",
"hostDetails":[
{
"hostname":"FLT",
"status":"UP",
"path":"dfdf",
"host":"dfdfdf",
"port":"1112"
},
{
"hostname":"SHP",
"status":"DOWN",
"path":"dfdfdf",
"host":"fgfgfg",
"port":"1112"
}
]
}
]
}
]
}
]
}
]
}
芒果更新:我正在使用express.js添加/更新和删除MangoDB中的条目。我在嵌套数组的更新中遇到了这个问题。请参阅下面的代码以更新嵌套数组。
router.post('/appDomain/update/:appDomainName/:envName', function(req, res, next) {
AppDomain.findOne({}, {_id: 0, appDomain: {$elemMatch: {appDomainName: req.params.appDomainName,}}}, function (err, appDomain) {
if(appDomain.appDomain[0].dashboard[0].envName == req.params.envName )
appDomain.appDomain[0].dashboard[0].envDetails.push({})
appDomain.save(function (err) {
if(err) {
console.error('ERROR!');
}
});
res.json(appDomain);
});
})
但是,它没有更新。如果有人能帮助我的话会很棒。
AppDomainModel架构
var Schema = mongoose.Schema; var serverDetails = new Schema({ hostname: String, ip: String, status: String, path: String, host: String, port: String }); var hostDetail = new Schema({ hostHdrName: String, hostDetails: [serverDetails] }); var keyValue = new Schema({ keyHdrName: String, keyValueData: [{key: String, value:String}] }); var envSchema = new Schema({ hostnme: String, ip: String, cpu: String, memory: String, disk: String, downtime: String, version: String, hostDetails: [hostDetail], keyValues: [keyValue], activities:{ recent: [keyValue], planned: [keyValue] } }); var dashboardSchema = new Schema({ envName: String, envDetails: [envSchema] }); var appDomainSchema = new Schema({ _id:String, appDomain:[{ appDomainName: {type:String,index: { unique: true }}, dashboard: [dashboardSchema] }] }); var AppDomain = mongoose.model('AppDomain', appDomainSchema); var Dashboard = mongoose.model('Dashboard', dashboardSchema); var EnvSchema = mongoose.model('EnvSchema', envSchema);
更新后。我正在使用以下函数检查值。但是更新后的数据在数据库中不可用。
router.get('/app/domain/get/:appDomainName', function(req, res, next) { AppDomain.find({}, {_id: 0, appDomain: {$elemMatch: {appDomainName: req.params.appDomainName,}}},function (err, appDomain) { res.json(appDomain); }); });
答案 0 :(得分:1)
经过长时间的斗争,我想通了。见下面的答案。
var data2 = {}; AppDomain.update( {_id:1,'appDomain.appDomainName': req.params.appDomainName,'dashboard.$.envName':{$nin:[req.params.envName]}}, {$push: {'appDomain.$.dashboard.0.envDetails':{'envDetails':[data2]}}}, function(err, model) { console.log(err); } );
答案 1 :(得分:0)
我怀疑您的更改实际上已成功保存,但您很快就会返回响应。试试这个:
appDomain.save(function (err, savedAppDomain) {
if(err) {
console.error('ERROR!');
next(err);
return;
}
res.json(savedAppDomain):
});
// Removed:
//res.json(appDomain);
//});