Javascript Node.js - 与猫鼬建立关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7810892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:41:20  来源:igfitidea点击:

Node.js - Creating Relationships with Mongoose

javascriptmongodbnode.jsmongoose

提问by donald

I have 2 Schemas, Custphoneand Subdomain. Custphonebelongs_toa Subdomainand Subdomainhas_manyCustphones.

我有 2 个架构,CustphoneSubdomain. Custphonebelongs_to一个SubdomainSubdomainhas_manyCustphones

The problem is in creating the relationship using Mongoose. My goal is to do: custphone.subdomain and get the Subdomain that the Custphone belongs to.

问题在于使用 Mongoose 创建关系。我的目标是: custphone.subdomain 并获取 Custphone 所属的子域。

I have this in my schemas:

我的模式中有这个:

SubdomainSchema = new Schema
    name : String

CustphoneSchema = new Schema
    phone : String
    subdomain  : [SubdomainSchema]

When I print the Custphone result I get this:

当我打印 Custphone 结果时,我得到了这个:

{ _id: 4e9bc59b01c642bf4a00002d,
  subdomain: [] }

When the Custphoneresult has {"$oid": "4e9b532b01c642bf4a000003"}in MongoDB.

Custphone结果{"$oid": "4e9b532b01c642bf4a000003"}在 MongoDB 中时。

I want to do custphone.subdomainand get the subdomain object of the custphone.

我想做custphone.subdomain并获取客户电话的子域对象。

回答by Dan

It sounds like you're looking to try the new populatefunctionality in Mongoose.

听起来您希望在 Mongoose 中尝试新的填充功能。

Using your example above:

使用上面的示例:

var Schema = mongoose.Schema,
    ObjectId = Schema.ObjectId;

SubdomainSchema = new Schema
    name : String

CustphoneSchema = new Schema
    phone : String
    subdomain  : { type: ObjectId, ref: 'SubdomainSchema' }

The subdomainfield will be is updated with an '_id' such as:

subdomain字段将使用“_id”进行更新,例如:

var newSubdomain = new SubdomainSchema({name: 'Example Domain'})
newSubdomain.save()

var newCustphone = new CustphoneSchema({phone: '123-456-7890', subdomain: newSubdomain._id})
newCustphone.save()

To actually get data from the subdomainfield you're going to have to use the slightly more complex query syntax:

要实际从subdomain字段中获取数据,您将不得不使用稍微复杂的查询语法:

CustphoneSchema.findOne({}).populate('subdomain').exec(function(err, custPhone) { 
// Your callback code where you can access subdomain directly through custPhone.subdomain.name 
})

回答by Connor Leech

I had a similar problem and had to use mongoose's Model.findByIdAndUpdate()

我有一个类似的问题,不得不使用猫鼬的 Model.findByIdAndUpdate()

docs: http://mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate

文档:http: //mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate

this post helped me also: http://blog.ocliw.com/2012/11/25/mongoose-add-to-an-existing-array/comment-page-1/#comment-17812

这篇文章也帮助了我:http: //blog.ocliw.com/2012/11/25/mongoose-add-to-an-existing-array/comment-page-1/#comment-17812