填充参考模型中的所有项目

时间:2019-09-27 23:55:38

标签: node.js mongodb mongoose mongoose-populate

我有一个这样的数据库:

enter image description here

我想从具有x ID的用户那里获取所有列表中的所有listItems。正确的做法是什么?

我正在使用带有猫鼬的节点,并且尝试了以下操作:

await User.findById(user._id).populate('list');

但是意识到我不能从中填充所有ListItem。意思是,我不能这样做:

await User.findById(user._id).populate('list').populate('listItem');

如何从具有x ID的用户那里获取所有列表中的所有listItem?

1 个答案:

答案 0 :(得分:1)

假设UserListListItem是集合,那么您应该可以使用$lookup来完成此操作。

Here is a live demo of the following query..


查询:

db.users.aggregate([
  {
    $match: {
      uniqueId: 1
    }
  },
  {
    $lookup: {
      from: "lists",
      localField: "uniqueId",
      foreignField: "userId",
      as: "lists"
    }
  },
  {
    $lookup: {
      from: "listItems",
      localField: "uniqueId",
      foreignField: "userId",
      as: "listItems"
    }
  }
])

数据集:

db={ // Simulates a DB  ********
  "users": [ // Simulates a Collection ********
    {
      "firstname": "John",
      "lastname": "Smith",
      "email": "jsmith@gmail.com",
      "password": "password123",
      "uniqueId": 1
    },
    {
      "firstname": "Jane",
      "lastname": "Doe",
      "email": "doe@yahoo.com",
      "password": "123password",
      "uniqueId": 2
    }
  ],
  "lists": [ // Simulates a Collection ********
    {
      "userId": 1,
      "name": "Johns List 1",
      "items": [
        11,
        12,
        13
      ]
    },
    {
      "userId": 2,
      "name": "Groceries",
      "items": [
        21,
        22,
        23
      ]
    }
  ],
  "listItems": [ // Simulates a Collection ********
    {
      "userId": 2,
      "itemId": 21,
      "title": "Apple",
      "notes": []
    },
    {
      "userId": 2,
      "itemId": 22,
      "title": "Banana",
      "notes": []
    },
    {
      "userId": 2,
      "itemId": 23,
      "title": "Strawberry",
      "notes": []
    },
    {
      "userId": 1,
      "itemId": 11,
      "title": "Oil Change",
      "notes": []
    },
    {
      "userId": 1,
      "itemId": 12,
      "title": "Pick Up Grandma",
      "notes": []
    },
    {
      "userId": 1,
      "itemId": 13,
      "title": "Go For Run",
      "notes": []
    }
  ]
}

结果:

[
  {
    "_id": ObjectId("5a934e000102030405000008"),
    "email": "jsmith@gmail.com",
    "firstname": "John",
    "lastname": "Smith",
    "listItems": [
      {
        "_id": ObjectId("5a934e000102030405000003"),
        "itemId": 11,
        "notes": [],
        "title": "Oil Change",
        "userId": 1
      },
      {
        "_id": ObjectId("5a934e000102030405000004"),
        "itemId": 12,
        "notes": [],
        "title": "Pick Up Grandma",
        "userId": 1
      },
      {
        "_id": ObjectId("5a934e000102030405000005"),
        "itemId": 13,
        "notes": [],
        "title": "Go For Run",
        "userId": 1
      }
    ],
    "lists": [
      {
        "_id": ObjectId("5a934e000102030405000006"),
        "items": [
          11,
          12,
          13
        ],
        "name": "Johns List 1",
        "userId": 1
      }
    ],
    "password": "password123",
    "uniqueId": 1
  }
]