Node、Express、MongoDB:使用对象数组创建模式?
Posted
技术标签:
【中文标题】Node、Express、MongoDB:使用对象数组创建模式?【英文标题】:Node, Express, MongoDB: Creating a schema with an array of objects? 【发布时间】:2020-03-23 09:34:48 【问题描述】:这是我想要的架构:
username: "taimoor",
pairs: [
code: "CA",
site: "google.ca"
,
code: "US",
site: "google.com"
,
code: "GB",
site: "google.co.uk"
]
date: 1574880349,
这是我当前的架构:
const redirectionSchema = new mongoose.Schema(
username:
type: String,
required: true,
,
pairs: [
// What do I place here?
],
date:
type: Date,
required: true,
default: Date.now
)
我不知道如何使用mongoose
继续和实现它。我应该创建另一个名为Pair
的架构吗?如果是这样:
Pair
添加到“pairs”?
感谢您的宝贵时间,这是我第一次使用 NoSQL。
【问题讨论】:
嗨,你检查我的回答了吗?如果是这样,请提供一些反馈是否有效。 【参考方案1】:您需要有一个用于对的内部架构。这样您就可以对这些对应用所需的验证或其他验证。
const mongoose = require("mongoose");
const redirectionSchema = new mongoose.Schema(
username:
type: String,
required: true
,
pairs: [
new mongoose.Schema(
code:
type: String,
required: true
,
site:
type: String,
required: true
)
],
date:
type: Date,
required: true,
default: Date.now
);
module.exports = mongoose.model("Redirect", redirectionSchema);
您可以使用以下 App.js 创建包含用户名和配对的文档,或者将另一个配对添加到现有文档中。
App.js
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const Redirect = require("./models/redirect");
const url = "mongodb://localhost:27017/redirectDB";
const port = process.env.PORT || 3000;
app.use(express.json());
mongoose
.connect(url,
useNewUrlParser: true,
useUnifiedTopology: true
)
.then(() =>
app.listen(port, () =>
console.log(`App running on port $port...`);
);
)
.catch(error => console.log(error));
app.post("/redirect", async (req, res) =>
try
const result = await Redirect.create(req.body);
res.send(result);
catch (err)
console.log(err);
res.status(500).send("Something went wrong");
);
app.post("/redirect/:id/pair", async (req, res) =>
const code, site = req.body;
try
const result = await Redirect.findByIdAndUpdate(
req.params.id,
$push:
pairs: code, site
,
new: true
);
res.send(result);
catch (err)
console.log(err);
res.status(500).send("Something went wrong");
);
要创建用户名和配对,请向 url http://localhost:3000/redirect
发送帖子请求,如下所示:
"username": "taimoor",
"pairs": [
"code": "CA",
"site": "google.ca"
,
"code": "US",
"site": "google.com"
]
响应会是这样的:
"_id": "5ddecfcb3cd5c035b4c31d95",
"username": "taimoor",
"pairs": [
"_id": "5ddecfcb3cd5c035b4c31d97",
"code": "CA",
"site": "google.ca"
,
"_id": "5ddecfcb3cd5c035b4c31d96",
"code": "US",
"site": "google.com"
],
"date": "2019-11-27T19:34:35.781Z",
"__v": 0
稍后我们可以使用对这个 url http://localhost:3000/redirect/5ddecfcb3cd5c035b4c31d95/pair
的 post 请求向这个文档添加一个新的对。 url 中的 id 是我们之前创建的并在响应中得到的 id。
请求:
"code": "GB",
"site": "google.co.uk"
回复:
"_id": "5ddecfcb3cd5c035b4c31d95",
"username": "taimoor",
"pairs": [
"_id": "5ddecfcb3cd5c035b4c31d97",
"code": "CA",
"site": "google.ca"
,
"_id": "5ddecfcb3cd5c035b4c31d96",
"code": "US",
"site": "google.com"
,
"_id": "5dded01693be502168a0f794",
"code": "GB",
"site": "google.co.uk"
],
"date": "2019-11-27T19:34:35.781Z",
"__v": 0
【讨论】:
非常感谢。 $push 对我有用。知道如何使每对的code
值唯一吗?我现在正在尝试解决这个问题,这非常困难
@TaimoorAhmad 我明天再看,但也许你可以提出另一个新问题,以便其他人也可以回复。
好主意,我会这样做的。再次感谢您的帮助。【参考方案2】:
型号
const redirectionSchema = new mongoose.Schema(
username:
type: String,
required: true,
,
pairs: [
_id: false,
code: type: String ,
site: type: String
],
date:
type: Date,
required: true,
default: Date.now
)
const Redirection = mongoose.model('Redirection', redirectionSchema);
使用
const redirection = new Redirection(
username: "taimoor",
pairs: [
code: "CA",
site: "google.ca"
,
code: "US",
site: "google.com"
,
code: "GB",
site: "google.co.uk"
],
date: new Date(1574880349),
)
redirection.save();
示例数据库输出
"_id":
"$oid": "5ddecc4c6dd3760c40ff354b"
,
"username": "taimoor",
"pairs": [
"code": "CA",
"site": "google.ca"
,
"code": "US",
"site": "google.com"
,
"code": "GB",
"site": "google.co.uk"
],
"date":
"$date": "1970-01-19T05:28:00.349Z"
,
"__v": 0
【讨论】:
记得添加问题;) 谢谢。为什么该对的 _id 设置为 false?既然它不是架构,那不会没有效果吗? 设置_id为false是可选的,我是在数组中添加元素的时候做的,不会生成新的_id,看场景,一般用处不大。以上是关于Node、Express、MongoDB:使用对象数组创建模式?的主要内容,如果未能解决你的问题,请参考以下文章
Node.JS、Express 和 MongoDB :: 多个集合