0
array1.array2.status
을 어떻게 업데이트 할 수 있습니까?몽구스에서 매우 중첩 된 문서를 업데이트하십시오.
var SomeSchema = new mongoose.Schema({
status: Boolean,
array1: [{
array2[{
status: boolean
}]
}]
})
array1.array2.status
을 어떻게 업데이트 할 수 있습니까?몽구스에서 매우 중첩 된 문서를 업데이트하십시오.
var SomeSchema = new mongoose.Schema({
status: Boolean,
array1: [{
array2[{
status: boolean
}]
}]
})
첫째,이 같은 서브 스키마로 스키마를 재구성 좋을 것이다 : 그런 다음
var valueItemSchema = new mongoose.Schema({
_id: Number,
status: Boolean
});
var subItemSchema = new mongoose.Schema({
_id: Number,
array2: [valueItemSchema]
});
/*
//or this if you dont want _id in your nested documents
var valueItemSchema = new mongoose.Schema({
status: Boolean
}, {
_id: false
});
var subItemSchema = new mongoose.Schema({
array2: [valueItemSchema]
}, {
_id: false
});
*/
var itemSchema = new mongoose.Schema({
status: Boolean,
array1: [subItemSchema]
});
var valueItem = db.model('ValueItem', valueItemSchema);
var SubItem = db.model('SubItem', subItemSchema);
var Item = db.model('Item', itemSchema);
//create a new item
var myItem = new Item({
status: true,
array1: new SubItem({
_id: 0,
array2: [new valueItem({
_id: 0,
"status": true
}), new valueItem({
_id: 1,
"status": false
}), new valueItem({
_id: 2,
"status": true
})]
})
});
, 당신은 당신의 array2
_id
로의 요소를 업데이트 할 수 있습니다
Item.findOne({
status: true
}).exec(
function(err, result) {
result.array1.id(0).array2.id(0).status=false;
result.save(function(err) {
console.log(result)
});
}
);
또는 인덱스로 서브 문서의 _id
을 업데이트하지 않으려면 :
Item.findOne({
status: true
}).exec(
function(err, result) {
result.array1[0].array2[0].status=false;
result.save(function(err) {
console.log(result)
});
}
);
Here 전체 데모는
입니다.