gpt4 book ai didi

使用两个集合的 MongoDB 查询

转载 作者:行者123 更新时间:2023-12-03 02:46:35 25 4
gpt4 key购买 nike

我是 MongoDB 的新手,并且陷入了编写查询的任务中。我的问题陈述是我有两个集合

maening userId1=>follows userId2
+---------+---------+
| userId1 | userId2 |
+---------+---------+
| 1 | 2 |
+---------+---------+
| 1 | 3 |
+---------+---------+
| 1 | 4 |
+---------+---------+
| 2 | 3 |
+---------+---------+
| 3 | 1 |
+---------+---------+
second table is as follows
+---------+--------+----------------+
| tweetId | userId | tweetText |
+---------+--------+----------------+
| **** | 1 | Tweet By user1 |
+---------+--------+----------------+
| **** | 2 | Tweet By user2 |
+---------+--------+----------------+
| **** | 3 | Tweet By user3 |
+---------+--------+----------------+
| **** | 4 | Tweet By user4 |
+---------+--------+----------------+

我希望我的结果包含 user1 关注的用户的所有推文...??????

最佳答案

您需要像这样设计用户和推文架构:

用户:

const mongoose = require("mongoose");

const userSchema = new mongoose.Schema({
username: String,
follows: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}
]
});

module.exports = mongoose.model("User", userSchema);

推文:

const mongoose = require("mongoose");

const tweetSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
tweetText: String
});

module.exports = mongoose.model("Tweet", tweetSchema);

然后您可以使用以下代码,插入用户、推文,获取用户及其关注者以及用户关注者的推文:

索引.js

const express = require("express");
const app = express();
const mongoose = require("mongoose");
const url = "mongodb://localhost:27017/tweet"; //change url to your db
const User = require("./models/user");
const Tweet = require("./models/tweet");

const 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("/user", async (req, res) => {
let result = await User.create(req.body);
res.send(result);
});

app.get("/user/:userId", async (req, res) => {
const result = await User.findById(req.params.userId).populate({
path: "follows",
select: "_id username"
});
res.send(result);
});

app.post("/user/:userId/follow/:followedUserId", async (req, res) => {
const result = await User.findByIdAndUpdate(
req.params.userId,
{
$push: {
follows: req.params.followedUserId
}
},
{ new: true }
);
res.send(result);
});

app.post("/tweet", async (req, res) => {
let result = await Tweet.create(req.body);
res.send(result);
});

app.get("/user/:userId/followedTweets", async (req, res) => {
const user = await User.findById(req.params.userId);

const result = await Tweet.find({ userId: { $in: user.follows } });
res.send(result);
});

首先创建一些用户,向此网址发送 POST http://localhost:3000/user:

请求:

{
"username": "username1"
}

然后使用以下网址向给定用户添加关注者:

http://localhost:3000/user/5ddfdfaf8c62b141146cbcff/follow/5ddfdfd18c62b141146cbd02

这意味着 ID 为 5ddfdfaf8c62b141146cbcff 的用户关注用户 5ddfdfd18c62b141146cbd02

此时,您可以让用户及其关注的用户向此网址发送 GET 请求:

http://localhost:3000/user/5ddfdfaf8c62b141146cbcff

响应将如下所示:

{
"follows": [
{
"_id": "5ddfdfbf8c62b141146cbd00",
"username": "username2"
},
{
"_id": "5ddfdfc78c62b141146cbd01",
"username": "username3"
},
{
"_id": "5ddfdfd18c62b141146cbd02",
"username": "username4"
}
],
"_id": "5ddfdfaf8c62b141146cbcff",
"username": "username1",
"__v": 0
}

从该响应中,我们了解到 username1 跟随 username2、username3 和 username4。

然后您可以创建推文,发送 POST 到此网址:http://localhost:3000/tweet

请求:

{
"userId": "5ddfdfbf8c62b141146cbd00",
"tweetText": "user 2 tweet 1"
}

回应:

{
"_id": "5ddfe1e7911cec475093f623",
"userId": "5ddfdfbf8c62b141146cbd00",
"tweetText": "user 2 tweet 1",
"__v": 0
}

添加几条这样的推文后,您可以向此网址发送 GET 请求来获取用户关注的用户的推文:

http://localhost:3000/user/5ddfdfaf8c62b141146cbcff/followedTweets

响应将如下所示:

[
{
"_id": "5ddfe1e7911cec475093f623",
"userId": "5ddfdfbf8c62b141146cbd00",
"tweetText": "user 2 tweet 1",
"__v": 0
},
{
"_id": "5ddfe20a911cec475093f624",
"userId": "5ddfdfbf8c62b141146cbd00",
"tweetText": "user 2 tweet 2",
"__v": 0
},
{
"_id": "5ddfe22b911cec475093f625",
"userId": "5ddfdfc78c62b141146cbd01",
"tweetText": "user 3 tweet 1",
"__v": 0
}
]

关于使用两个集合的 MongoDB 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59087579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com