question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add option to merge rather than overwrite schema-level `populate()` options

See original GitHub issue

Prerequisites

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

Issue

hi, i have two schemas with below descriptions

User Schema

class User {
    @Prop() _id: number;
    @Prop() events: object; // it has two keys : create and delete
}

Custom Schema

class Custom {
    @Prop() _id: number;
    @Prop() user_id: number
}

CustomSchema.virtual('user' , {
  ref: 'user',
  localField: 'user_id',
  foreignField: '_id',
  options: {
      match: {
          'events.delete': null
      }
  }
})

custom schema has relation with user which is defined via a virtual.

How to produce problem

run this query :

custom.findOne({ _id:1 }).populate({ path: 'user, match: {'events.create': {$ne : null } } }).exec();

Expected behavior

populate user if events.delete is null and events.create is not null

Result

populate user if events.create is not null

What is the problem?

match defined in populate overwrites match defined in virtual. how to append match defined in populate with match defined in virtual?

thanks.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
vkarpov15commented, Oct 28, 2022

We should add an option to merge rather than overwrite properties in populate().

0reactions
vkarpov15commented, Oct 28, 2022

Right now we don’t have an easy workaround. The best option would be to store the schema-level match as a variable, and merge it manually:

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
  name: String,
  events: {}
});

const otherSchema = new mongoose.Schema({
  userId: {type: mongoose.Schema.Types.ObjectId, ref: 'Test'}
}, {toObject: {virtuals: true}});

const userVirtualMatch = { 'events.delete': null };
otherSchema.virtual('user', {
  ref: 'Test',
  localField: 'userId',
  foreignField: '_id',
  options: { match: userVirtualMatch}
});

const Test = mongoose.model('Test', testSchema);

const Other = mongoose.model('Other', otherSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  const entry = await Test.create({
    name: 'Test',
    events: { delete: null, create: 'hi'}
  });

  const other = await Other.create({
    userId: entry._id
  });
  await Other.create();


  const result = await Other.findOne({ _id: other._id }).populate({path: 'user', match: {...userVirtualMatch, 'events.create': {$ne: null}}});
  console.log(result);
  console.log(result.user[0].events);
}

run();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Appending/Overwriting with Different Schema to Delta Lake ...
Attempting to add data to a Delta file that has different schema ( different column names, differnt data types, etc) will cause Delta...
Read more >
Diving Into Delta Lake: Schema Enforcement & Evolution
Learn how schema enforcement and schema evolution work together on Delta Lake to ensure high quality, reliable data.
Read more >
Setting crawler configuration options - AWS Glue
If you are configuring the crawler on the console, to combine schemas, select the crawler option Create a single schema for each S3...
Read more >
MERGE (Transact-SQL) - SQL Server - Microsoft Learn
Runs insert, update, or delete operations on a target table from the ... The MERGE statement can't update the same row more than...
Read more >
4. Spark SQL and DataFrames: Introduction to Built-in Data ...
In Python from pyspark.sql import SparkSession # Create a SparkSession spark ... Instead of having a separate metastore for Spark tables, Spark by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found