我有以下文件:
{
"month": 2,
"year": 2019,
"categories": [
{
"title": "Income",
"budget": 5000,
"amount": 2000,
"subcategories": [
{
"title": "Paychecks",
"budget": 4000,
"amount": 1000,
"transactions": [
{
"day": 3,
"amount": 600,
"vendor": "Microsoft",
"memo": "Paycheck 1"
},
{
"day": 10,
"amount": 400,
"vendor": "Google",
"memo": "Paycheck 2"
}
]
},
{
"title": "Other",
"budget": 1000,
"amount": 1000,
"transactions": [
{
"day": 7,
"amount": 600,
"vendor": "Mom",
"memo": "Gift for Baby"
},
{
"day": 19,
"amount": 400,
"vendor": "Amy",
"memo": "Gift for Julie"
}
]
}
]
},
{
"title": "Housing",
"budget": 3000,
"amount": 3000,
"subcategories": [
{
"title": "Rent",
"budget": 2000,
"amount": 2000,
"transactions": [
{
"day": 1,
"amount": 2000,
"vendor": "Homes4Rent",
"memo": "Rent"
}
]
},
{
"title": "Electric",
"budget": 1000,
"amount": 1000,
"transactions": [
{
"day": 14,
"amount": 1000,
"vendor": "Tampa Electric",
"memo": "Electric"
}
]
}
]
}
]}
我想从一个非常特定的类别中删除一个非常特定的子类别。例如,我想从该文档的“房屋”类别中删除“出租”子类别。
我尝试了各种查询,但是无论它们在哪个类别中,它们都只会删除名为“ Rent”的所有子类别。我正在学习聚合框架,并且已经成功地将其用于其他查询,但是我对这个框架并不太幸运。
这是到目前为止我能提出的最好的查询:
db.collection.update(
{
"year":2019,
"month":2,
"categories.title":"Housing",
"categories.subcategories.title":"Rent"
},
{
$pull:{
"categories.$.subcategories":"Rent"
}
}
)
不幸的是,这导致:
WriteResult({"nMatched":1, "nUpserted":0, "nModified":0})
如果我需要进一步澄清任何内容,请告诉我。非常感谢。