在嵌套数组 MongoDB 中更新或插入新数据
Update or Insert new data in nested array MongoDB
我正在使用 Node JS 和 Mongoose fyi。鉴于我有这样的事情:-
let people = [
{
"_id": 1,
"name": "Person 1",
"pets": [
{
"_id": 1,
"name": "Tom",
"category": "cat",
"favFood": [
{
"_id": 1,
"name": "steamed fish"
}
]
},
{
"_id": 2,
"name": "Jerry",
"category": "mouse",
"favFood": [
{
"_id": 1,
"name": "cheese"
}
]
}
]
}
]
现在我知道如何insert
新pet
数据如下所示:-
// new Pet Data to INSERT
let newPetData = {
name: "Butch",
category: "dog",
favFood: [
{
name: "bone"
}
]
}
// INSERT new data in DB
People.updateOne(
// push into the sub-documents - pets
{ $push: { pets: newPetData } },
(err, success) => {
if(err) res.json({ message: `Unsuccessful!`, report: err })
else res.json({ message: `Successful!`, report: success })
}
)
但是,如果我有 id
(宠物的 ID),我该如何插入 pet
的新 favFood
数据?
let petID = 1 // this is the ID of pet named Tom
// this is a new favorite food of Tom to INSERT
let newFavFood = {
name: "fried fish"
}
// code to INSERT new favorite food of Tom (THIS DOES NOT WORK!)
People.updateOne(
{ "_id": petID } // id of pet 'Tom'
{ $push: { favFood: newFavFood } }, // push into the sub-documents - favFood
(err, success) => {
if(err) res.json({ message: `Unsuccessful!`, report: err })
else res.json({ message: `Successful!`, report: success })
}
)
以上代码无效。我想如果我指定宠物的 id
就像上面的节目 { "_id": petID }
会产生我想要的。尽管它 returns 是 Successful!
的 message
。但是 没有新记录添加到 db 本身。我要寻找的结果是 如下所示 :-
let people = [
{
"_id": 1,
"name": "Person 1",
"pets": [
{
"_id": 1,
"name": "Tom",
"category": "cat",
"favFood": [
{
"_id": 1,
"name": "steamed fish"
},
{
"_id": 2,
"name": "fried fish" // this is the new data inserted
}
]
},
{
"_id": 2,
"name": "Jerry",
"category": "mouse",
"favFood": [
{
"_id": 1,
"name": "cheese"
}
]
}
]
}
]
有没有其他方法可以做到这一点?我必须在其中实施 $elemMatch
吗?如果是这样,我怎么能在这里做到这一点?
查询部分匹配文档,而不是元素,因此 { "_id": petID }
匹配人员的 _id。要匹配宠物的,请使用点分符号,例如 { "pets._id": petID }
然后使用位置运算符$
引用匹配查询部分的第一个数组元素:
.updateOne(
{ "pets._id": petID },
{ $push: { "pets.$.favFood": newFavFood }}
)
我正在使用 Node JS 和 Mongoose fyi。鉴于我有这样的事情:-
let people = [
{
"_id": 1,
"name": "Person 1",
"pets": [
{
"_id": 1,
"name": "Tom",
"category": "cat",
"favFood": [
{
"_id": 1,
"name": "steamed fish"
}
]
},
{
"_id": 2,
"name": "Jerry",
"category": "mouse",
"favFood": [
{
"_id": 1,
"name": "cheese"
}
]
}
]
}
]
现在我知道如何insert
新pet
数据如下所示:-
// new Pet Data to INSERT
let newPetData = {
name: "Butch",
category: "dog",
favFood: [
{
name: "bone"
}
]
}
// INSERT new data in DB
People.updateOne(
// push into the sub-documents - pets
{ $push: { pets: newPetData } },
(err, success) => {
if(err) res.json({ message: `Unsuccessful!`, report: err })
else res.json({ message: `Successful!`, report: success })
}
)
但是,如果我有 id
(宠物的 ID),我该如何插入 pet
的新 favFood
数据?
let petID = 1 // this is the ID of pet named Tom
// this is a new favorite food of Tom to INSERT
let newFavFood = {
name: "fried fish"
}
// code to INSERT new favorite food of Tom (THIS DOES NOT WORK!)
People.updateOne(
{ "_id": petID } // id of pet 'Tom'
{ $push: { favFood: newFavFood } }, // push into the sub-documents - favFood
(err, success) => {
if(err) res.json({ message: `Unsuccessful!`, report: err })
else res.json({ message: `Successful!`, report: success })
}
)
以上代码无效。我想如果我指定宠物的 id
就像上面的节目 { "_id": petID }
会产生我想要的。尽管它 returns 是 Successful!
的 message
。但是 没有新记录添加到 db 本身。我要寻找的结果是 如下所示 :-
let people = [
{
"_id": 1,
"name": "Person 1",
"pets": [
{
"_id": 1,
"name": "Tom",
"category": "cat",
"favFood": [
{
"_id": 1,
"name": "steamed fish"
},
{
"_id": 2,
"name": "fried fish" // this is the new data inserted
}
]
},
{
"_id": 2,
"name": "Jerry",
"category": "mouse",
"favFood": [
{
"_id": 1,
"name": "cheese"
}
]
}
]
}
]
有没有其他方法可以做到这一点?我必须在其中实施 $elemMatch
吗?如果是这样,我怎么能在这里做到这一点?
查询部分匹配文档,而不是元素,因此 { "_id": petID }
匹配人员的 _id。要匹配宠物的,请使用点分符号,例如 { "pets._id": petID }
然后使用位置运算符$
引用匹配查询部分的第一个数组元素:
.updateOne(
{ "pets._id": petID },
{ $push: { "pets.$.favFood": newFavFood }}
)