Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self populating doesn't work in 6.1.8 #11289

Closed
AndreyKomin opened this issue Jan 28, 2022 · 5 comments
Closed

Self populating doesn't work in 6.1.8 #11289

AndreyKomin opened this issue Jan 28, 2022 · 5 comments
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@AndreyKomin
Copy link

const historySchema = new mongoose.Schema(
  {
    plan: Object,
    parents: [this],
    isDeleted: Boolean,
    isDraft: Boolean,
  },
  {
    timestamps: true,
  }
)

export default mongoose.model("history", historySchema)

Look at parents. It worked perfectly in ^5.13.14 but not in ^6.1.8.
I tried some ways to make it work but unfortunately without any effect.

@vkarpov15
Copy link
Collaborator

What is this in your example?

Also, what is the expected behavior versus the observed behavior?

@vkarpov15 vkarpov15 added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Jan 30, 2022
@AndreyKomin
Copy link
Author

AndreyKomin commented Jan 31, 2022

Think that this is referring to itself.

I want to do await History.findById(historyId).populate("parents") and with the example above it worked perfectly in ^5.13.14. So I was able to populate another history items in parents, referring to itself in the Schema.

Now, in ^6.1.8 it no longer works

@vkarpov15
Copy link
Collaborator

"this is referring to itself" <-- no idea what this means. I suspect that this is either null or global object.

Please provide a complete repro script that demonstrates the behavior you're seeing.

@AndreyKomin
Copy link
Author

@vkarpov15
Hi there, sorry for delay. Providing the script.
So try to run it under ^5.13.14 and then under ^6.2.2 and then compare output of console.log(result)

const mongoose = require("mongoose")
const MONGO_URI = "mongodb://localhost:27017/test-issue"

const historySchema = new mongoose.Schema({
  data: String,
  parents: [this],
})

const History = mongoose.model("history", historySchema)

const run = async () => {
  await mongoose.connect(MONGO_URI)

  await History.deleteMany({}) // Freshening the collection

  const parentHistoryItem = await History.create({ parents: [], data: "Parent data in here" })
  const parentObjectId = parentHistoryItem._id

  const childHistoryItem = await History.create({ parents: [parentObjectId], data: "Child data" })
  const childObjectId = childHistoryItem._id

  const result = await History.findById(childObjectId).populate("parents")

  console.log(result)
}

;(async function () {
  await run()
})()

@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity labels Feb 24, 2022
@vkarpov15 vkarpov15 added this to the 6.2.7 milestone Feb 24, 2022
@vkarpov15
Copy link
Collaborator

@AndreyKomin the script as written worked by accident, and isn't a feature that Mongoose supports. The problem is that, in your script, Mongoose 5 interpretted [this] to be [], an array of any type. The correct way to define your schema is as follows:

const historySchema = new mongoose.Schema({
  data: String,
  parents: [{ type: mongoose.ObjectId, ref: 'history' }],
})

In Mongoose 6, we also don't fall back to using the History model when using Query.prototype.populate(). We'll add a note to our migration guide about this. But, in the meantime, please change how parents is defined in your schema and that should fix this issue.

@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Mar 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

No branches or pull requests

2 participants