Snippets Collections
const VoteSchema = new Schema({
  value: Boolean,
  obj: String,
  kind: String,
  user: String,
  voted: String
});

const PostSchema = new Schema({
  title: String
});

var virtual = PostSchema.virtual('voted', {
  ref: 'Vote',
  localField: '_id',
  foreignField: 'obj',
  justOne: true,
});

virtual.getters.unshift(v => !!v);

const Vote = mongoose.model('Vote', VoteSchema);
const Post = mongoose.model('Post', PostSchema);

co(function * () {
    // create post
  const post = new Post({
    title: 'post-1'
  });

  // create vote
  const vote = new Vote({
    value: true,
    obj: post.id,
    kind: Post.modelName,
    user: 'uid:1'
  });

  // store models to db
  yield [
    post.save(),
    vote.save()
  ];

  // populate related votes
  const r = yield Post.findById(post._id)
    .populate({path: 'voted', match: {user: 'uid:1', kind: Post.modelName}});

  console.log('r', r.toObject({ virtuals: true }));
  console.log(r.voted);
});
userSchema.pre('find', function (next) {
  if (this.options._recursed) {
    return next();
  }
  this.populate({ path: "followers following", options: { _recursed: true } });
  next();
});
star

Tue Aug 22 2023 18:37:17 GMT+0000 (Coordinated Universal Time)

#python #commandline #conda #virtual #environment
star

Thu Feb 16 2023 14:32:06 GMT+0000 (Coordinated Universal Time) https://github.com/Automattic/mongoose/issues/4832

#mongodb #getter #setter #virtual
star

Thu Feb 16 2023 11:39:22 GMT+0000 (Coordinated Universal Time) https://mongoosejs.com/docs/populate.html#populate-virtuals

#mongodb #virtual #populate
star

Tue Jul 19 2022 05:30:59 GMT+0000 (Coordinated Universal Time) https://realpython.com/python-virtual-environments-a-primer/

#activate #venv #python #virtual

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension