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

Follow up to "populated documents get saved as strings" #14827

Closed
2 tasks done
fardolieri opened this issue Aug 23, 2024 · 0 comments · Fixed by #14833
Closed
2 tasks done

Follow up to "populated documents get saved as strings" #14827

fardolieri opened this issue Aug 23, 2024 · 0 comments · Fixed by #14833

Comments

@fardolieri
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.5.2

Node.js version

20.15.1

MongoDB server version

7.0.12

Typescript version (if applicable)

No response

Description

There is an edge case that we missed in #14759. When calling populate on a document the _id is again stored as a string in the DB.

I was able to track it down to this line. But this may not be the whole picture.

 function convertTo_id(val, schema) { 
   if (val != null && val.$__ != null) { 
-    return val._id; 
+    return val._doc._id;
   } 

Steps to Reproduce

It's the same example as in #14759 but I added pet.populate('owner') before pet.save().

mongoose.Schema.ObjectId.get(
  value => {
    if (value?.constructor?.name?.toLowerCase() === 'objectid') return String(value)
    return value
  }
)

const ownerSchema = new mongoose.Schema({ name: 'String' })
const petSchema = new mongoose.Schema({
  name: 'String',
  owner: { type: 'ObjectId', ref: 'owner' }
})

const Owner = mongoose.model('owner', ownerSchema)
const Pet = mongoose.model('pet', petSchema)

const owner = new Owner({ name: 'Alice' })
const pet = new Pet({ name: 'Kitty', owner: owner })

await owner.save()

console.log(typeof pet._doc.owner.$__.wasPopulated.value) // object 😀
await pet.populate('owner') // This breaks it 💥💥💥💥💥💥💥💥💥💥
console.log(typeof pet._doc.owner.$__.wasPopulated.value) // string 🙁

await pet.save()

/**
 * The property 'owner' of 'pet' has unexpectedly been saved
 * as a string into the mongoDB instead of ObjectId.
 * 
 * No matter how I query for it, mongoose is not
 * able to find it...
 */

const reloadAttempts = await Promise.all([
  Pet.findOne({ owner: owner }),
  Pet.findOne({ owner: owner._id }),
  Pet.findOne({ owner: String(owner._id) }),
  Pet.findOne({ owner: new mongoose.Types.ObjectId(owner._id) }),
])

console.log(reloadAttempts) // Logs [ null, null, null, null ] but shouldn't

Expected Behavior

No response

vkarpov15 added a commit that referenced this issue Aug 28, 2024
…ng schemas to avoid modifying original obj

Fix #14827
vkarpov15 added a commit that referenced this issue Aug 28, 2024
fix(populate): fix a couple of other places where we get the document's _id with getters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant