如何使用Mongoose在另一个集合中查找子查询文档。

huangapple go评论95阅读模式
英文:

How to lookup subquery documents with another collection moongoose

问题

我需要两次加入用户集合,因为同一用户将发布产品并将购买用户的详细信息作为子文档。

产品模式:

  1. {
  2. product_title: String,
  3. product_desc: String,
  4. Product_purchased:[
  5. {
  6. userid: mongoose.Schema.ObjectId,
  7. purchased_date: Date
  8. }]
  9. posteduserId: mongoose.Schema.ObjectId
  10. }

用户模式:

  1. {
  2. _id: userId,
  3. name: String,
  4. Pic: String
  5. }

示例文档:

  1. [
  2. {
  3. "product_title":"title",
  4. "product_desc":"desc",
  5. "Product_purchased":[
  6. {
  7. "userid":"5d4hvh7duc7c7c8d9scbe",
  8. "name":"name",
  9. "Pic":"url",
  10. "purchased_date":"Date"
  11. },
  12. {
  13. "userid":"5d4hvh7duc7c7c8d9scbe",
  14. "name":"name",
  15. "Pic":"url",
  16. "puchased_date":"Date"
  17. }
  18. ],
  19. "posteduserId": "5d4hvh7duc7c7c8d9scbe",
  20. "userid": "5d4hvh7duc7c7c8d9scbe",
  21. "name": "name",
  22. "pic": "url",
  23. }
  24. ]

将相同用户表与发布的用户ID和已购买用户ID的子数组连接。

请帮助我如何解决这个问题,提前感谢。

英文:

I need to join user collections two times as same user will post product and purchased user details as sub document.

Product Schema:

  1. {
  2. product_title: String,
  3. product_desc: String,
  4. Product_purchased:[
  5. {
  6. userid: mongoose.Schema.ObjectId,
  7. purchased_date: Date
  8. }]
  9. posteduserId: mongoose.Schema.ObjectId
  10. }

Users Schema:

  1. {
  2. _id: userId,
  3. name: String,
  4. Pic: String
  5. }

Example document:

  1. [
  2. {
  3. "product_title":"title",
  4. "product_desc":"desc",
  5. "Product_purchased":[
  6. {
  7. "userid":"5d4hvh7duc7c7c8d9scbe",
  8. "name":"name",
  9. "Pic":"url",
  10. "purchased_date":"Date"
  11. },
  12. {
  13. "userid":"5d4hvh7duc7c7c8d9scbe",
  14. "name":"name",
  15. "Pic":"url",
  16. "puchased_date":"Date"
  17. }
  18. ],
  19. "posteduserId": "5d4hvh7duc7c7c8d9scbe",
  20. "userid": "5d4hvh7duc7c7c8d9scbe",
  21. "name": "name",
  22. "pic": "url",
  23. }
  24. ]

Join same user table with posted userid and subarray of purchased userIds.

Please help me how to crack this one, Thanks in advance.

答案1

得分: 1

你需要修复你的产品模式,像这样包含 ref 字段:

  1. const mongoose = require("mongoose");
  2. const ProductSchema = new mongoose.Schema({
  3. product_title: String,
  4. product_desc: String,
  5. Product_purchased: [
  6. {
  7. userid: {
  8. type: mongoose.Schema.ObjectId,
  9. ref: "User"
  10. },
  11. purchased_date: Date
  12. }
  13. ],
  14. posteduserId: {
  15. type: mongoose.Schema.ObjectId,
  16. ref: "User"
  17. }
  18. });
  19. module.exports = mongoose.model("Product", ProductSchema);

我设置用户模式如下:

  1. const mongoose = require("mongoose");
  2. const UserSchema = new mongoose.Schema({
  3. name: String,
  4. Pic: String
  5. });
  6. module.exports = mongoose.model("User", UserSchema);

ref 中的 "User" 值必须与用户模型中的模型名称 "User" 匹配。

然后你需要像这样两次填充:

  1. router.get("/products", async (req, res) => {
  2. const result = await Product.find({})
  3. .populate("Product_purchased.userid")
  4. .populate("posteduserId");
  5. res.send(result);
  6. });

假设你有这3个用户:

  1. {
  2. "_id": "5e133deb71e32b6a68478ab4",
  3. "name": "user1 name",
  4. "Pic": "user1 pic",
  5. "__v": 0
  6. },
  7. {
  8. "_id": "5e133df871e32b6a68478ab5",
  9. "name": "user2 name",
  10. "Pic": "user2 pic",
  11. "__v": 0
  12. },
  13. {
  14. "_id": "5e133e0271e32b6a68478ab6",
  15. "name": "user3 name",
  16. "Pic": "user3 pic",
  17. "__v": 0
  18. }

和这个产品:

  1. {
  2. "_id": "5e133e9271e32b6a68478ab8",
  3. "product_title": "product1 title",
  4. "product_desc": "product1 description",
  5. "Product_purchased": [
  6. {
  7. "purchased_date": "2020-01-06T14:02:14.029Z",
  8. "_id": "5e133e9271e32b6a68478aba",
  9. "userid": "5e133df871e32b6a68478ab5"
  10. },
  11. {
  12. "purchased_date": "2020-01-06T14:02:14.029Z",
  13. "_id": "5e133e9271e32b6a68478ab9",
  14. "userid": "5e133e0271e32b6a68478ab6"
  15. }
  16. ],
  17. "posteduserId": "5e133deb71e32b6a68478ab4",
  18. "__v": 0
  19. }

结果将会是:

  1. [
  2. {
  3. "_id": "5e133e9271e32b6a68478ab8",
  4. "product_title": "product1 title",
  5. "product_desc": "product1 description",
  6. "Product_purchased": [
  7. {
  8. "purchased_date": "2020-01-06T14:02:14.029Z",
  9. "_id": "5e133e9271e32b6a68478aba",
  10. "userid": {
  11. "_id": "5e133df871e32b6a68478ab5",
  12. "name": "user2 name",
  13. "Pic": "user2 pic",
  14. "__v": 0
  15. }
  16. },
  17. {
  18. "purchased_date": "2020-01-06T14:02:14.029Z",
  19. "_id": "5e133e9271e32b6a68478ab9",
  20. "userid": {
  21. "_id": "5e133e0271e32b6a68478ab6",
  22. "name": "user3 name",
  23. "Pic": "user3 pic",
  24. "__v": 0
  25. }
  26. }
  27. ],
  28. "posteduserId": {
  29. "_id": "5e133deb71e32b6a68478ab4",
  30. "name": "user1 name",
  31. "Pic": "user1 pic",
  32. "__v": 0
  33. },
  34. "__v": 0
  35. }
  36. ]
英文:

First you need to fix your Product schema like this to include the ref field:

  1. const mongoose = require("mongoose");
  2. const ProductSchema = new mongoose.Schema({
  3. product_title: String,
  4. product_desc: String,
  5. Product_purchased: [
  6. {
  7. userid: {
  8. type: mongoose.Schema.ObjectId,
  9. ref: "User"
  10. },
  11. purchased_date: Date
  12. }
  13. ],
  14. posteduserId: {
  15. type: mongoose.Schema.ObjectId,
  16. ref: "User"
  17. }
  18. });
  19. module.exports = mongoose.model("Product", ProductSchema);

I setup user model like this:

  1. const mongoose = require("mongoose");
  2. const UserSchema = new mongoose.Schema({
  3. name: String,
  4. Pic: String
  5. });
  6. module.exports = mongoose.model("User", UserSchema);

The ref User value must match the model name User in the user model.

Then you need to populate two times like this:

  1. router.get("/products", async (req, res) => {
  2. const result = await Product.find({})
  3. .populate("Product_purchased.userid")
  4. .populate("posteduserId");
  5. res.send(result);
  6. });

Let's say you have this 3 users:

  1. {
  2. "_id": "5e133deb71e32b6a68478ab4",
  3. "name": "user1 name",
  4. "Pic": "user1 pic",
  5. "__v": 0
  6. },
  7. {
  8. "_id": "5e133df871e32b6a68478ab5",
  9. "name": "user2 name",
  10. "Pic": "user2 pic",
  11. "__v": 0
  12. },
  13. {
  14. "_id": "5e133e0271e32b6a68478ab6",
  15. "name": "user3 name",
  16. "Pic": "user3 pic",
  17. "__v": 0
  18. }

And this product:

  1. {
  2. "_id": "5e133e9271e32b6a68478ab8",
  3. "product_title": "product1 title",
  4. "product_desc": "product1 description",
  5. "Product_purchased": [
  6. {
  7. "purchased_date": "2020-01-06T14:02:14.029Z",
  8. "_id": "5e133e9271e32b6a68478aba",
  9. "userid": "5e133df871e32b6a68478ab5"
  10. },
  11. {
  12. "purchased_date": "2020-01-06T14:02:14.029Z",
  13. "_id": "5e133e9271e32b6a68478ab9",
  14. "userid": "5e133e0271e32b6a68478ab6"
  15. }
  16. ],
  17. "posteduserId": "5e133deb71e32b6a68478ab4",
  18. "__v": 0
  19. }

The result will be:

  1. [
  2. {
  3. "_id": "5e133e9271e32b6a68478ab8",
  4. "product_title": "product1 title",
  5. "product_desc": "product1 description",
  6. "Product_purchased": [
  7. {
  8. "purchased_date": "2020-01-06T14:02:14.029Z",
  9. "_id": "5e133e9271e32b6a68478aba",
  10. "userid": {
  11. "_id": "5e133df871e32b6a68478ab5",
  12. "name": "user2 name",
  13. "Pic": "user2 pic",
  14. "__v": 0
  15. }
  16. },
  17. {
  18. "purchased_date": "2020-01-06T14:02:14.029Z",
  19. "_id": "5e133e9271e32b6a68478ab9",
  20. "userid": {
  21. "_id": "5e133e0271e32b6a68478ab6",
  22. "name": "user3 name",
  23. "Pic": "user3 pic",
  24. "__v": 0
  25. }
  26. }
  27. ],
  28. "posteduserId": {
  29. "_id": "5e133deb71e32b6a68478ab4",
  30. "name": "user1 name",
  31. "Pic": "user1 pic",
  32. "__v": 0
  33. },
  34. "__v": 0
  35. }
  36. ]

huangapple
  • 本文由 发表于 2020年1月6日 21:24:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定