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.

Find query at child

See original GitHub issue

I am trying to use a test case as described in “mongoose-schema-extend”

All is working just as explained there.

But, I would expect it to give me the ability to do a search query on the inherited type. So if we consider the example showed in the link above:

var VehicleSchema = mongoose.Schema({ 
  make : String,
}, { collection : 'vehicles', discriminatorKey : '_type' });

var CarSchema = VehicleSchema.extend({
  year : Number
});
var BusSchema = VehicleSchema.extend({
  route : Number
})

var Vehicle = mongoose.model('vehicle', VehicleSchema),
    Car = mongoose.model('car', CarSchema),
    Bus = mongoose.model('bus', BusSchema);

var accord = new Car({ 
  make : 'Honda',
  year : 1999
});
var muni = new Bus({
   make : 'Neoplan',
  route : 33
});

I would expect Car.find({}) to return only the documents which have _type : Car. Instead, I get all vehicles.

Is there a way to get only the cars except for doing Car.find{"_type":"Car"})?

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
nodesworkcommented, Oct 24, 2016

Another quick implementation is to use pre hooks to inject the discriminatorKey into conditions:

 99     var patchDiscriminatorKey = function() {
100       this._conditions[key] = this.model.modelName;
101     };
102
103     newSchema.pre('find', patchDiscriminatorKey);
104     newSchema.pre('findOne', patchDiscriminatorKey);
105     newSchema.pre('count', patchDiscriminatorKey);
106     newSchema.pre('findOneAndRemove', patchDiscriminatorKey);
107     newSchema.pre('findOneAndUpdate', patchDiscriminatorKey);
108     newSchema.pre('update', patchDiscriminatorKey);
0reactions
sandeepaboutcliniccommented, May 5, 2017

i add above line. after that facing some problem.

Hi, i have schemas. person staff extend person provider extend person

staff.save() saving data with Discriminator Key __t =‘sfaff’ provider.save() saving data with Discriminator Key __t =‘provider’ person.save() saving data without Discriminator Key

in plugin i did changes

` var patchDiscriminatorKey = function() { this._conditions[key] = this.model.modelName; };

newSchema.pre(‘find’, patchDiscriminatorKey); newSchema.pre(‘findOne’, patchDiscriminatorKey); newSchema.pre(‘count’, patchDiscriminatorKey); newSchema.pre(‘findOneAndRemove’, patchDiscriminatorKey); newSchema.pre(‘findOneAndUpdate’, patchDiscriminatorKey); newSchema.pre(‘update’, patchDiscriminatorKey);

return newSchema; }; ` let me know what is issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Query a Parent-Child Tree in SQL | LearnSQL.com
Example 1: List All Children of 1 Parent ... This is the simplest query, so I'll use it to make you more at...
Read more >
Has child query | Elasticsearch Guide [8.5] | Elastic
(Required, query object) Query you wish to run on child documents of the type field. If a child document matches the search, the...
Read more >
Searching Nested Child Documents - Apache Solr
Children Query Parser​​ Can be used to retrieve children of a matching document. For a detailed explanation of this parser, see the section...
Read more >
parent-child & child-parent soql query custom object
Hi I have university as parent object & college as child object. There is a master-detail relationship defined on child object i.e college...
Read more >
.children() | jQuery API Documentation
children () method allows us to search through the children of these elements in the DOM tree and construct a new jQuery object...
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