正确的猫鼬子文档关系

时间:2018-07-01 15:35:58

标签: mongodb mongoose relationship

我有一个模特

const userSchema = Schema({
  username: String
})

const invitationSchema = Schema({
  decision: String,
  user: {
    type: Schema.ObjectId,
    ref: 'User'
  }
})

const groupSchema = Schema({
  name: String,
  invitations: [{
    type: Schema.ObjectId,
    ref: 'Invitation'
  }]
})

所以基本模型看起来像

group {
  name
  invitations [
     user {
       username
     }
  ]
}

作为用户,我希望获得邀请的所有群组

const user = getContextUser()
Group.find({
  'invitations.user': user._id
})

但是它似乎不起作用,我可以使用一个属性组来引用Group模型中的Invitation模式,但是我想要一个好的方法

如何获取用户邀请的所有群组

别在下面看这个,这是我在json中的实际模型表示,以防万一

// GROUP
{
  "_id": ObjectId("5b3901dd9a8c0e6790af4ee8"),
  "image": {
    "filename": "8547aa2a-d16c-4da7-8d51-6c9bf8038037.jpg",
    "width": 236,
    "height": 156
  },
  "geolocation": {
    "coordinates": [-152.1997, -24.7718],
    "type": "Point"
  },
  "invitations": [
    ObjectId("5b3901dd9a8c0e6790af4eea"),
    ObjectId("5b3901dd9a8c0e6790af4eeb"),
    ObjectId("5b3901dd9a8c0e6790af4eec"),
    ObjectId("5b3901dd9a8c0e6790af4eed"),
    ObjectId("5b3901dd9a8c0e6790af4eee"),
    ObjectId("5b3901dd9a8c0e6790af4eef"),
    ObjectId("5b3901dd9a8c0e6790af4ef0")
  ],
  "messages": [],
  "title": "id in magnam",
  "description": "Cupiditate doloremque sunt placeat beatae et ex rerum nisi voluptate. Aliquam hic voluptas quas iure assumenda rerum aut. Quisquam vero beatae odit aut ut quod magnam.",
  "created_at": ISODate("2018-07-01T16:31:25.237Z"),
  "created_by": ObjectId("5b3901dc9a8c0e6790af4ee1"),
  "color": "#347303",
  "require_people_decision": true,
  "is_public": true,
  "start_date": ISODate("2019-03-18T20:05:03.116Z"),
  "end_date": ISODate("2019-03-18T20:05:03.116Z"),
  "location": "25498 Berniece Prairie",
  "__v": 1
}

// INVITATION
{
  "_id": ObjectId("5b3901dd9a8c0e6790af4eea"),
  "created_at": ISODate("2018-07-01T16:31:25.348Z"),
  "have_seen": false,
  "user": ObjectId("5b3901dc9a8c0e6790af4ee0"),
  "__v": 0
}

// USER 
{
  "_id": ObjectId("5b3901dc9a8c0e6790af4ee0"),
  "avatar": {
    "filename": "d438befb-2ea3-4582-b6bf-b8dd3b9bcf66.jpeg",
    "width": 200,
    "height": 200
  },
  "phone": "5556106679",
  "password": "$2b$10$mbx9Yhj5mAKyVxwmxcumaepXYkCTiF/9VM2KJRARZkPOVN300pKc.",
  "version": 1,
  "friends": [],
  "__v": 0
}

1 个答案:

答案 0 :(得分:1)

您可以尝试以下汇总

Group.aggregate([
  { "$lookup": {
    "from": Invitations.collection.name,
    "let": { "invitations": "$invitations" },
    "pipeline": [
      { "$match": { "$expr": { "$in": [ "$_id", "$$invitations" ] } } }
    ],
    "as": "invitations"
  }},
  { "$match": { "invitations.user": user._id } }
])

或此

Group.aggregate([
  { "$lookup": {
    "from": Invitations.collection.name,
    "let": { "invitations": "$invitations" },
    "pipeline": [
      { "$match": {
        "user": user._id,
        "$expr": { "$in": [ "$_id", "$$invitations" ] }
      }}
    ],
    "as": "invitations"
  }},
  { "$match": { "invitations.user": user._id } }
])

最后一个我认为是从User集合开始的最佳选择

User.aggregate([
  { "$match": { "_id": user._id }},
  { "$lookup": {
    "from": Groups.collection.name,
    "let": { "userId": "$_id" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": [ "$created_by", "$$userId" ] }}}
    ],
    "as": "groups"
  }}
])