node.js - 如何将数据推送到nodejs mongodb中的另一个模式
问题描述
谢谢你点击我的问题..
我在 mongodb 中有两个模式,citySchema 和destinationSchema。我想建立一对多的关系。每个目的地都有一个城市,而这个城市有很多目的地。
这是我的目的地架构
var mongoose = require("mongoose");
var destinationSchema = new mongoose.Schema({
name: String,
image: String,
description: String,
city: {
type: mongoose.Schema.Types.ObjectId,
ref: "City"
}
});
module.exports = mongoose.model("Destination", destinationSchema);
这是城市架构
var mongoose = require("mongoose");
var citySchema = new mongoose.Schema({
name: String,
image: String,
description: String,
destinations: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Destination"
}
]
});
module.exports = mongoose.model("City", citySchema);
这是创建新目的地的发布路线
router.post("/", function(req, res) {
var name = req.body.name;
var image = req.body.image;
var description = req.body.description;
var city = req.body.city;
var newDestination = {
name: name,
image: image,
description: description,
city: city
};
Destination.create(newDestination, function(err, newCreatedDestination) {
if (err) {
console.log(err);
} else {
res.redirect("/admin/destinations");
}
});
});
这是我创建新目的地的表格
<select name="city">
<% cities.forEach(function(city) { %>
<option value=<%=city._id%> > <%= city.name %> </option>
<% }) %>
</select>
它工作正常。但是我希望当我创建一个新目的地时,它将当前目的地 ID 推送到城市模式(目的地数组)。
对不起,我的英语不好。我感谢您的每一个回答和建议。谢谢你
解决方案
创建新目的地后,您可以将该目的地的 ID 推送到城市目的地。
您可以使用以下路线:
router.post("/", async (req, res) => {
try {
const { name, image, description, city } = req.body;
let newDestination = { name, image, description, city };
newDestination = await Destination.create(newDestination);
let result = await City.findByIdAndUpdate(
city,
{
$push: { destinations: newDestination._id }
},
{ new: true }
);
res.redirect("/admin/destinations");
} catch (err) {
console.log(err);
res.status(500).send("Something went wrong");
}
});
让我们有一个这样的现有城市:
{
"destinations": [],
"_id": "5e071f212e9ecd31508785c6",
"name": "Padang",
"image": "image 1",
"description": "beautiful city",
"__v": 0
}
如果我们想将此城市添加为目的地,我们可以将此请求正文发送到我们的发布路线。请注意,作为城市值,我们需要使用现有的城市 id ( 5e071f212e9ecd31508785c6
),就像我们已经拥有的一样。
{
"name": "destination name",
"image": "destination image",
"description": "destination description",
"city": "5e071f212e9ecd31508785c6"
}
结果将是这样的:
创建了一个新目标:
{
"_id" : ObjectId("5e071fd51cd3600bb083b5e7"),
"name" : "destination name",
"image" : "destination image",
"description" : "destination description",
"city" : ObjectId("5e071f212e9ecd31508785c6"),
"__v" : NumberInt(0)
}
并添加到城市:
{
"_id" : ObjectId("5e071f212e9ecd31508785c6"),
"destinations" : [
ObjectId("5e071fd51cd3600bb083b5e7")
],
"name" : "Padang",
"image" : "image 1",
"description" : "beautiful city",
"__v" : NumberInt(0)
}
推荐阅读
- java - Java List `of` 方法相当混乱
- sql - 连接/创建到 Microsoft SQL Server 的本地实例
- excel - 函数头自动突出显示错误 (VBA)
- python - 循环遍历 excel 工作表 - 数组索引超出范围
- ios - 尽管强制解包 IBOutlets,类没有初始化器?
- javascript - Puppeteer:向下滚动推特时间线停止
- python - 在 Python Flask 中,如何获取当前登录用户的角色名称?
- ionic-framework - 仅在 Firestore 中获取具有子 ID 的嵌套集合数据
- python - 在浏览器中打开 Jupyter Notebook 时出现问题
- azure - Office 365 for AD B2C 应用开发团队