Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find query at child #26

Open
shaikatzir opened this issue Sep 9, 2014 · 9 comments
Open

Find query at child #26

shaikatzir opened this issue Sep 9, 2014 · 9 comments
Assignees
Labels

Comments

@shaikatzir
Copy link

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"})?

@talha-asad
Copy link

+1

3 similar comments
@karaxuna
Copy link

+1

@loudwinston
Copy link

+1

@jsgandalf
Copy link

+1

@loudwinston
Copy link

I've forked the project and am working on implementing this. Will issue a pull request soon.

@loudwinston
Copy link

If anyone wants to take a look a progress so far, and possibly contribute, the fork is at https://github.com/okpanda/mongoose-schema-extend

Note that this hasn't been extensively tested yet.

@sieira
Copy link
Collaborator

sieira commented Nov 11, 2015

I will check it, thanks for your work @loudwinston

@sieira sieira added the bug label Nov 11, 2015
@sieira sieira self-assigned this Nov 11, 2015
@nodeswork
Copy link

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);

@sandeepaboutclinic
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants