Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

mongodb - DbRef with Mongoose - mongoose-dbref or populate?

I have the following 2 schemas:

Company Event:

var companyEventSchema = new Schema({
    name : String,
    description
    date : Date,
    attendees : [ { type : Schema.ObjectId, ref : 'Member' } ],
]});

And Member

var memberSchema = new Schema({
    name : String,
    emailAddress: String,
    password :String,
    created: { type: Date, default: Date.now }
});

Is the way i've ref'd Member from companyEventSchema correct? I'm trying to do something a long the lines of a dbref.

I saw theres a separate project for that though... mongoose-dbref

However, the mongoose docs say the above provides "dbref like functionality"

Which would be more efficient?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You only need to use an actual DBRef (and mongoose-dbref) for the case where a field can contain ObjectIds that reference documents in potentially more than one collection. A DBRef is a tuple of an ObjectId, a collection name, and an optional database name.

Mongoose ref: fields, however, contain just an ObjectId and it's the Mongoose schema that defines what one collection the ObjectIds reference.

So Mongoose ref: fields are more efficient and should always be used unless you need the multi-collection reference support that DBRef provides.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...