Skip to content

Handle Mongoose reference fields #27

@NeoTheThird

Description

@NeoTheThird

Since this lib uses the default client to talk to mongodb, mongoose references are not understood.

If i use a model like this:

const SomeModel = mongoose.model(
  "Some",
  mongoose.Schema({
    data: { type: String },
    other: { type: "ObjectId", ref: "Other" }
  })
);

const OtherModel = mongoose.model(
  "Other",
  mongoose.Schema({
    whatever: { type: String }
  })
);

Would return something like this:

{
  _id: "ObjectId(1337)",
  data: "here be dragons",
  other: "ObjectId(1234)"
}

Instead of what you would expect:

{
  _id: "ObjectId(1337)",
  data: "here be dragons",
  other: {
    _id: "ObjectId(1234)"
  }
}

Obviously it's possible to work around this by re-mapping the object in js (which is ugly but works) or by using mongoose's methods rather than the default findOneById (which makes it impossible to use the built-in cache). Maybe it could be an option to use have findOneById actually use mongoose for find operations, if the constructor was passed a model instead of a collection? There would be more benefits, such as the possibility to cache Mongoose's virtuals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions