Coder Social home page Coder Social logo

Comments (4)

neohotsauce avatar neohotsauce commented on June 16, 2024

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.

IslandRhythms avatar IslandRhythms commented on June 16, 2024

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.

vkarpov15 avatar vkarpov15 commented on June 16, 2024

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.

vkarpov15 avatar vkarpov15 commented on June 16, 2024

Fix will be in Mongoose 7.4.4.

from mongoose-autopopulate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.