多对多关系 - 在查询中填充相关数据 - Keystonejs

时间:2022-10-04 16:10:26

I am working on a keystonejs project here and am running into a problem with relationships between two models.

我正在这里开发一个keystonejs项目,并且遇到了两个模型之间关系的问题。

I have the below two models:

我有以下两种型号:

User model:

User.add({
    name: { type: Types.Name, required: true, index: true },
    email: { type: Types.Email, initial: true, required: true, index: true },
  number: { type: Types.Number, index: true },
    password: { type: Types.Password, initial: true, required: true }
}, 'Permissions', {
    isAdmin: { type: Boolean, label: 'Can access Keystone', index: true },
}, 'Groups', {
    groups: {type: Types.Relationship, ref: 'Group', many: true }
});

And I have a Group model

我有一个集团模型

Group.add({
    name: { type: String, required: true, index: true },
    description: { type: String, initial: true, required: true, index: true}

});

I have a aggregate function that basically pulls all of the groups that have users in it

我有一个聚合函数,基本上拉动了所有拥有用户的组

    User.model.aggregate({$group:{'_id':'$groups'}}).exec(function(err,data){
                console.log(data);
});

My problem is that the above aggregate only shows me my groups by their _id values and not their names.

我的问题是上面的聚合只通过它们的_id值显示我的组,而不是它们的名字。

How could I populate and show the names in the front end? Obviously the id's are necessary on the back end to reference but to the front end users that won't work.

我怎么能填充并显示前端的名称?显然,后端需要id才能引用,但前端用户无法工作。

Thanks

1 个解决方案

#1


2  

You can create what you want pretty easily so don't be discouraged. You just need a heads up on the '.populate()' function in mongoose. See example below.

你可以很容易地创建你想要的东西,所以不要气馁。你只需要在mongoose中使用'.populate()'函数。见下面的例子。

User Model (slightly tidied - I removed the (strange?) nesting)

用户模型(稍微整理 - 我删除了(奇怪的?)嵌套)

User.add({
    name: { type: Types.Name, required: true, index: true },
    email: { type: Types.Email, initial: true, required: true, index: true},
    number: { type: Types.Number, index: true },
    password: { type: Types.Password, initial: true, required: true },
    isAdmin: { type: Boolean, label: 'Can access Keystone', index: true },
    groups: {type: Types.Relationship, ref: 'Group', many: true }
});

Group Model -- note the Group.relationship({}) option I've added near the bottom for admin convenience (shows users which reference that group on the backend)

组模型 - 请注意我为了方便管理而在底部附近添加了Group.relationship({})选项(显示在后端引用该组的用户)

Group.add({
    name: { type: String, required: true, index: true },
    description: { type: String, initial: true, required: true, index: true}    
});

Group.relationship({ ref: 'User', path: 'users', refPath:'Groups'});

Controller getting a list of users with all their corresponding group information

控制器获取具有所有相应组信息的用户列表

 view.on('init', function(next) {
    keystone.list('User').model.find()
    .populate('groups')
    .exec(function(err, users) {
      if(err) {
        console.log(err);
        return next(err);
      } else {
        locals.data.users = users;
        next(err);
      }
    });
  });

Controller getting Users within a specific group to display on the frontend (you need the group ID first)

控制器使特定组内的用户显示在前端(首先需要组ID)

  view.on('init', function(next) {
    keystone.list('User').model.find()
    .where('Groups').in([array, of, group, ids])
    .populate('groups')
    .exec(function(err, users) {
      if(err) {
        console.log(err);
        return next(err);
      } else {
        locals.data.users = users;
        next(err);
      }
    });
  });

locals.data.users would return like this in each case:

locals.data.users会在每种情况下都这样返回:

[
  {
    _id: '',
    name: '',
    ...
    groups: [
      {
        _id: '',
        name: ''
        ...
      }
    ]
  }
]

#1


2  

You can create what you want pretty easily so don't be discouraged. You just need a heads up on the '.populate()' function in mongoose. See example below.

你可以很容易地创建你想要的东西,所以不要气馁。你只需要在mongoose中使用'.populate()'函数。见下面的例子。

User Model (slightly tidied - I removed the (strange?) nesting)

用户模型(稍微整理 - 我删除了(奇怪的?)嵌套)

User.add({
    name: { type: Types.Name, required: true, index: true },
    email: { type: Types.Email, initial: true, required: true, index: true},
    number: { type: Types.Number, index: true },
    password: { type: Types.Password, initial: true, required: true },
    isAdmin: { type: Boolean, label: 'Can access Keystone', index: true },
    groups: {type: Types.Relationship, ref: 'Group', many: true }
});

Group Model -- note the Group.relationship({}) option I've added near the bottom for admin convenience (shows users which reference that group on the backend)

组模型 - 请注意我为了方便管理而在底部附近添加了Group.relationship({})选项(显示在后端引用该组的用户)

Group.add({
    name: { type: String, required: true, index: true },
    description: { type: String, initial: true, required: true, index: true}    
});

Group.relationship({ ref: 'User', path: 'users', refPath:'Groups'});

Controller getting a list of users with all their corresponding group information

控制器获取具有所有相应组信息的用户列表

 view.on('init', function(next) {
    keystone.list('User').model.find()
    .populate('groups')
    .exec(function(err, users) {
      if(err) {
        console.log(err);
        return next(err);
      } else {
        locals.data.users = users;
        next(err);
      }
    });
  });

Controller getting Users within a specific group to display on the frontend (you need the group ID first)

控制器使特定组内的用户显示在前端(首先需要组ID)

  view.on('init', function(next) {
    keystone.list('User').model.find()
    .where('Groups').in([array, of, group, ids])
    .populate('groups')
    .exec(function(err, users) {
      if(err) {
        console.log(err);
        return next(err);
      } else {
        locals.data.users = users;
        next(err);
      }
    });
  });

locals.data.users would return like this in each case:

locals.data.users会在每种情况下都这样返回:

[
  {
    _id: '',
    name: '',
    ...
    groups: [
      {
        _id: '',
        name: ''
        ...
      }
    ]
  }
]