Comments (4)
It works if select option is removed from the virtual. Works if the virtual is as below:
AddressSchema.virtual("residentials", {
ref: "Citizen",
localField: "_id",
foreignField: "permanentAddress.address",
justOne: false,
autopopulate: true,
match: {
status: "active"
}
});
from mongoose-autopopulate.
The virtual is not autopopulated while the document property is.
const mongoose = require('mongoose');
const AddressSchema = new mongoose.Schema(
{
name: {
type: String
},
status: {
type: String,
enum: ["active", "inactive"],
default: "active"
}
},
{
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true }
}
);
AddressSchema.virtual("residentials", {
ref: "Citizen",
localField: "_id",
foreignField: "permanentAddress.address",
justOne: false,
autopopulate: true,
match: {
status: "active"
},
options: {
select:
"name nId"
}
});
AddressSchema.plugin(require("mongoose-autopopulate"));
const CitizenSchema = new mongoose.Schema(
{
nId: {
type: String,
unique: true,
required: [true, "Please add national ID card"]
},
name: {
type: String,
required: [true, "Please add a name"],
trim: true
},
permanentAddress: {
name: {
type: String,
trim: true
},
address: {
type: mongoose.Schema.ObjectId,
ref: "Address"
},
},
father: {
type: mongoose.Schema.ObjectId,
refPath: "fatherType",
autopopulate: true
},
fatherType: {
type: String,
enum: ["Citizen", "Guest"],
required: true
},
status: {
type: String,
enum: ["active", "inactive"],
default: "active"
}
},
{
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true }
}
);
CitizenSchema.plugin(require("mongoose-autopopulate"));
const Address = mongoose.model('Address', AddressSchema);
const Citizen = mongoose.model('Citizen', CitizenSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
const entry = await Address.create({
name: "Another name for The Address",
status: "active"
});
const doc = await Citizen.create({
nId: 'Hello',
name: 'There',
permanentAddress: {
name: 'The Address',
address: entry._id
},
fatherType: "Guest"
});
const res = await Citizen.create({
nId: 'Yo',
name: 'Test',
permanentAddress: {
name: "The Address",
address: entry._id
},
father: doc._id,
fatherType: "Citizen",
status: "active"
});
console.log(await Citizen.find());
console.log(await Address.findOne())
}
run();
Output:
[
{
permanentAddress: {
name: 'The Address',
address: new ObjectId("63bc67003be77fc70e511bff")
},
_id: new ObjectId("63bc67003be77fc70e511c02"),
nId: 'Hello',
name: 'There',
fatherType: 'Guest',
status: 'active',
createdAt: 2023-01-09T19:12:00.278Z,
updatedAt: 2023-01-09T19:12:00.278Z,
__v: 0,
id: '63bc67003be77fc70e511c02'
},
{
permanentAddress: {
name: 'The Address',
address: new ObjectId("63bc67003be77fc70e511bff")
},
_id: new ObjectId("63bc67003be77fc70e511c04"),
nId: 'Yo',
name: 'Test',
father: {
permanentAddress: [Object],
_id: new ObjectId("63bc67003be77fc70e511c02"),
nId: 'Hello',
name: 'There',
fatherType: 'Guest',
status: 'active',
createdAt: 2023-01-09T19:12:00.278Z,
updatedAt: 2023-01-09T19:12:00.278Z,
__v: 0,
id: '63bc67003be77fc70e511c02'
},
fatherType: 'Citizen',
status: 'active',
createdAt: 2023-01-09T19:12:00.305Z,
updatedAt: 2023-01-09T19:12:00.305Z,
__v: 0,
id: '63bc67003be77fc70e511c04'
}
]
{
_id: new ObjectId("63bc67003be77fc70e511bff"),
name: 'Another name for The Address',
status: 'active',
createdAt: 2023-01-09T19:12:00.233Z,
updatedAt: 2023-01-09T19:12:00.233Z,
__v: 0,
residentials: [
{
permanentAddress: [Object],
_id: new ObjectId("63bc67003be77fc70e511c02"),
nId: 'Hello',
name: 'There',
id: '63bc67003be77fc70e511c02'
},
{
permanentAddress: [Object],
_id: new ObjectId("63bc67003be77fc70e511c04"),
nId: 'Yo',
name: 'Test',
father: new ObjectId("63bc67003be77fc70e511c02"),
id: '63bc67003be77fc70e511c04'
}
],
id: '63bc67003be77fc70e511bff'
}
from mongoose-autopopulate.
I took a closer look and the issue is that Mongoose's selectPopulatedFields
helper only adds the fields being populated to the projection. It also needs to add the field referenced by refPath
to the projection.
As a workaround, you can add fatherType
to the projection: select: "name nId fatherType"
from mongoose-autopopulate.
Fix will be in Mongoose 7.4.4.
from mongoose-autopopulate.
Related Issues (20)
- How to disable auto populate when create. HOT 2
- can't use mongoose-autopopulate on recursively nested schema : Maximum call stack size exceeded HOT 2
- please use semantic versioning HOT 1
- How to hide the selected field which is necessary for the virtual field in the result data HOT 3
- Add support for nested autopopulated virtuals HOT 3
- Unable to initiate Autopopulate on Schema.plugin() HOT 2
- Issue with autopopulating a schema on itself HOT 1
- Autopopulate not working with findByIdAndDelete
- Virtuals populate match works with object but not with function HOT 1
- Missing `schemaStack` argument in `eachPathRecursive` method calling? HOT 2
- Missing types for plugin configuration HOT 1
- Projection Select Not Working when i'm using mongoose-autopopulate HOT 2
- AutoPopulate on type.string HOT 1
- Provide native Typescript types (instead of `@types/mongoose-autopopulate`) HOT 1
- autopopulate across databases HOT 3
- Tests failing with mongoose 6.2.4 HOT 2
- populate is not working! HOT 1
- It takes a lots of time
- Last defined discriminator of nested array discriminators is not populated HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongoose-autopopulate.