用非id的字段填充猫鼬模型 [英] Populate a mongoose model with a field that isn't an id

查看:46
本文介绍了用非id的字段填充猫鼬模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以用不是_id的参考模型字段填充猫鼬模型...例如用户名.

Is it possible to populate a mongoose model with a field of a reference model that isn't the _id ... e.g. a username.

类似

var personSchema = Schema({
  _id     : Number,
  name    : String,
  age     : Number,
  stories : { type: String, field: "username", ref: 'Story' }
});

推荐答案

您可以使用populate() API. 该API更加灵活,您无需在架构中指定reffield.

You may use the populate() API. The API is more flexible, you don't have to specify ref and field in the Schema.

http://mongoosejs.com/docs/api.html#document_Document-populate http://mongoosejs.com/docs/api.html#model_Model.populate

您可以与find()混合搭配.

这篇关于用非id的字段填充猫鼬模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆