[英]Making a post/put request to an object inside an array
I have many documents like the following stored inside mongo DB.我在 mongo DB 中存储了许多类似以下的文档。
{
"posts": [
{
"_id": "5ee38b041385d900004e78de",
"postName": "Driver one",
"hasSublevels": false,
"isChildOfOther": false,
regions: []
},
{
"_id": "5ee38b0e1385d900004e78df",
"postName": "Driver 2",
"hasSublevels": true,
"isChildOfOther": false,
regions: []
},
{
"_id": "5ee38b1b1385d900004e78e0",
"postName": "Driver 3",
"hasSublevels": true,
"isChildOfOther": true,
regions: []
},
{
"_id": "5ee38b281385d900004e78e1",
"postName": "Driver 4",
"hasSublevels": true,
"isChildOfOther": true,
regions: []
},
{
"_id": "5ee38b3a1385d900004e78e2",
"postName": "Driver 5",
"hasSublevels": true,
"isChildOfOther": false,
regions: []
},
{
"_id": "5ee38b461385d900004e78e3",
"postName": "Driver 6",
"hasSublevels": true,
"isChildOfOther": true,
regions: []
}
],
"_id": "5ee38b499bd40260ad591d7e",
"name": "Cabs NY",
"date": "2020-06-12T14:03:53.343Z",
"__v": 0
},
Each post has a regions array as shown above.每个帖子都有一个区域数组,如上所示。 I would like to add values to the region field inside the posts array.
我想将值添加到帖子数组中的区域字段。 How do I make a request that first, retrieves a particular document by id?
如何发出请求,首先按 id 检索特定文档? Second, allows me to add a value(s) to the regions array in a particular post object?
其次,允许我向特定帖子 object 中的区域数组添加一个值吗?
This code for first part, the second part you can use id.regions then push the data into it like what I did.此代码用于第一部分,第二部分您可以使用 id.regions 然后像我一样将数据推送到其中。
result ={ "posts": [ { "_id": "5ee38b041385d900004e78de", "postName": "Driver one", "hasSublevels": false, "isChildOfOther": false, regions: [] }, { "_id": "5ee38b0e1385d900004e78df", "postName": "Driver 2", "hasSublevels": true, "isChildOfOther": false, regions: [] }, { "_id": "5ee38b1b1385d900004e78e0", "postName": "Driver 3", "hasSublevels": true, "isChildOfOther": true, regions: [] }, { "_id": "5ee38b281385d900004e78e1", "postName": "Driver 4", "hasSublevels": true, "isChildOfOther": true, regions: [] }, { "_id": "5ee38b3a1385d900004e78e2", "postName": "Driver 5", "hasSublevels": true, "isChildOfOther": false, regions: [] }, { "_id": "5ee38b461385d900004e78e3", "postName": "Driver 6", "hasSublevels": true, "isChildOfOther": true, regions: [] } ] } results = result.posts; groups = {}; for (var i in results) { var groupName = results[i]._id; if (.groups[results[i];_id]) { groups[groupName] = []. } groups[groupName]:push({"regions".results[i];regions}). } console;log(groups);
const id = "5ee38b3a1385d900004e78e2";
const post = posts.find(post => post._id === id);
const payload = {city: Nairobi, Country: Kenya};
post.regions.push(payload);
const post = Posts.findById(id);
if(!post) res.status(404).json({
msg: "Specified post not found."
});
post.regions.push(payload)
post.save()
return new post.返回新帖子。
According to the MongoDB documentationhttps://docs.mongodb.com/manual/reference/operator/update/push/ you can use the $push operator to add a value to an array much like Alpha's example.根据 MongoDB 文档https://docs.mongodb.com/manual/reference/operator/update/push/您可以使用类似于数组的 $push' 运算符来添加值。
That would look like那看起来像
db.posts.update(
{ _id: "5ee38b281385d900004e78e1" },
{ $push: { regions: "West" } }
)
Or to append multiple或 append 多个
db.posts.update(
{ _id: "5ee38b281385d900004e78e1" },
{ $push: { regions: { $each: ["West", "East", "South"] } } }
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.