简体   繁体   English

在 Mongoose 上填充模式

[英]Populate Schema on Mongoose

I have two collections one is users and another is cats.我有两个 collections 一个是用户,另一个是猫。 I want cats data under my users collection.我想要我的用户集合下的猫数据。 But I am not getting.但我没有得到。

User.js用户.js

var Schema = mongoose.Schema;
var userSchema = Schema({
_id: Schema.Types.ObjectId,
username: String,
email: { type: String, unique: true, lowercase: true, trim: true },
password: String,
role: String,
cats: [{ type: Schema.ObjectId, ref: 'Cat' }]
});
var User = mongoose.model('User', userSchema);
exports.default = User;

Cat.js猫.js

var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var catSchema = Schema({
fname: String,
mname: String,
lname: String,
});
var Cat = mongoose.model('Cat', catSchema);
exports.default = Cat;

O/p开/关

"_id": ObjectId("5a446ab43533970b8489e1ac"), "username": "xyz", "email": "xyz@gmail.com", "password""$2a$10$ogerY6OiCRKy9TjPYERaOugUJeqelBl.yToJ4ZBX3ac2MVZQpsKOu", "role": "user", "cats": [ ], "__v": 0 “_id”:ObjectId(“5a446ab43533970b8489e1ac”),“用户名”:“xyz”,“电子邮件”:“xyz@gmail.com”,“密码”“$2a$10$ogerY6OiCRKy9TjPYERaOugUJeqelBl.yToJ4ZBX3ac2MVZQpsKOu”,“角色”:“用户", "猫": [ ], "__v": 0

Expected Output预计 Output

"_id": ObjectId("5a446ab43533970b8489e1ac"), "username": "xyz", "email": "xyz@gmail.com", "password""$2a$10$ogerY6OiCRKy9TjPYERaOugUJeqelBl.yToJ4ZBX3ac2MVZQpsKOu", "role": "user", "cats": [{ "_id": ObjectId("5a44707effc66a234447c36b"), "fname": "felix", "mname": "", "lname": "", } ], "__v": 0 “_id”:ObjectId(“5a446ab43533970b8489e1ac”),“用户名”:“xyz”,“电子邮件”:“xyz@gmail.com”,“密码”“$2a$10$ogerY6OiCRKy9TjPYERaOugUJeqelBl.yToJ4ZBX3ac2MVZQpsKOu”,“角色”:“用户", "cats": [{ "_id": ObjectId("5a44707effc66a234447c36b"), "fname": "felix", "mname": "", "lname": "", } ], "__v": 0

You might combine them into just one schema like below. 您可以将它们组合为仅一种如下所示的架构。 No any reason to create schemas separately unless the array count increases a couple of hundreds continuously.. 除非数组数量连续增加数百个,否则没有任何理由单独创建架构。

var Schema = mongoose.Schema;
var userSchema = Schema({
  _id: Schema.Types.ObjectId,
  username: String,
  email: { type: String, unique: true, lowercase: true, trim: true },
  password: String,
  role: String,
  cats: [
    { 
      _id: Schema.Types.ObjectId,
      fname: String,
      mname: String,
      lname: String,
    }
  ]
});
var User = mongoose.model('User', userSchema);
exports.default = User;

修复您的架构(应为cats: [{ type: Schema.Types.ObjectId, ref: 'Cat' }]并参考http://mongoosejs.com/docs/populate.html

You can get data like this, using model.find().populate('ref')您可以使用model.find().populate('ref')获取这样的数据

const story = await User.find().populate('Cat');

It will return all the users and against each user, an array of respective Cat .它将返回所有用户并针对每个用户返回相应的Cat数组。 You already mentioned the ref of the Cat in the User model , so you don't need to do anything extra.您已经在User model中提到了Catref ,因此您不需要做任何额外的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM