使用两个集合的MongoDB查询

我对使用MongoDB并不陌生,而且我一直在忙于编写query的任务。 我的问题陈述是我有两个收藏夹

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遵循的来自用户的所有推文... ?????

emergtech 回答:使用两个集合的MongoDB查询

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

用户:

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);

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

Index.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",res) => {
  const result = await User.findById(req.params.userId).populate({
    path: "follows",select: "_id username"
  });
  res.send(result);
});

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

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

app.get("/user/:userId/followedTweets",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

此时,您可以让一个用户及其关注的用户向此URL发送一个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
}

根据此回复,我们了解到用户名1紧随用户名2,用户名3和用户名4。

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

请求:

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

响应:

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

添加了一些这样的推文后,您可以发送GET请求此URL,以获取用户关注的用户的推文:

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

响应如下:

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

您可以使用聚合来实现特定的结果。 就像您有2个收藏集一样,first_collection和second_colleciton。

db.getCollection('first_collecition').aggregate([
{
        "$facet":{
            "tweets":[
                "$lookup":{
                    "from":"second_collection","localField":"userId1","foreignField":"userId","as":"tweets"
                },{
                    "$project":{
                        "userId":"$userId","tweets":"tweetText"
                    }
                }
            ]
        }
    }

])

本文链接:https://www.f2er.com/3014712.html

大家都在问