我无法解决此问题(并希望避免循环更新),请在此处帮助我。
我有一个fambio文档(具有自己的FamilyModel),该文档是在用户提供以下信息后创建的:
<select [(ngModel)]="rejectionReason">
<!-- Notice I have used empty string for the select option,
which makes sense to me while disabled checking -->
<option value="" selected disabled>Select</option>
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
</select>
<!-- Since empty strings are "falsey",you can
use ! directly on the rejection reason. -->
<button type="button" [disabled]="!rejectionReason">Submit</button>
现在,保存完上述信息后,用户在后端进行一些处理后即可提供有关家庭的更多信息:
ngFor
FamilyModel模式总的来说如下:
{
name: 'john',
lname: 'doe',
}
现在,John有一个家庭对象数组(文件类型为Array),并试图像这样插入该对象数组:
我尝试了多种选择的方式:
let familyArr = [
{ _id: 1234, name: 'Jenny', lname: 'doe', relation: 'mother' },
{ _id: 2345, name: 'Jawn', lname: 'doe', relation: 'father' },
{ _id: 3456, name: 'Jane', lname: 'doe', relation: 'sister' },
{ _id: 4567, name: 'Daisy', lname: 'wick', relation: 'pupper' }
]
const FamilyModel = mongoose.Schema({
name: {type: String, required: true},
lname: {type: String, required: true},
family: [relationshipSchema]
}, {
timestamp: true
});
const relationshipSchema = mongoose.Schema({
name: {type: String, required: true},
lname: {type: String, required: true},
relation: {type: String, required: true}
}, {
required: false,
timestamp: true
});
db.fambio.updateOne({_id: 1111}, { $set: { family: familyArr }})
关于将构造的对象直接插入字段没有任何作用。一次插入一个时,它会更新。
我应该如何编写查询以将对象数组更新/追加到维护其自身架构的Array Type字段中。
答案 0 :(得分:0)
好的,我已经解决了。我是怎么解决的:
最初,我保存了文档,并根据我的需求帖子对信息进行了一些处理,我想将元素数组插入到我正在遇到的未插入任何问题的数组键字段中。我所缺少的是$ each,这就是我的做法:
db.getCollection('fambio').update({
_id: johnsuserid
}, {
$push: {
'family': {
$each:[
{ _id: 1234, name: 'Jenny', lname: 'doe', relation: 'mother' },
{ _id: 2345, name: 'Jawn', lname: 'doe', relation: 'father' },
{ _id: 3456, name: 'Jane', lname: 'doe', relation: 'sister' },
{ _id: 4567, name: 'Daisy', lname: 'wick', relation: 'pupper' }
]
}
}
});
谢谢大家!!希望这对将来可能会遇到此问题的人有所帮助。