Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to search multiple Mongoose models at once?

I have four Mongoose models, SoleTrader, Partnership, Company and Trust. They’re different enough that I can’t merge them all into one schema, yet similar enough that I regularly need to query or make changes to all 4 types at once and rarely care which sort they are.

Is there a way of doing this – possibly by putting all four types in a single collection – without making four database calls each time?

like image 278
cbnz Avatar asked Oct 01 '13 08:10

cbnz


People also ask

Can Mongoose queries be chained?

The Mongoose Query class provides a chaining interface for finding, updating, and deleting documents.

Is Mongoose faster than MongoDB?

Mongoose, a neat ODM library for MongoDB used in Node. js projects, has plenty of useful features that make developers' lives easier. It manages relationships between data, has schema validation, and overall allows coding 3-5 times faster.

How do you retrieve data from multiple collections in MongoDB node JS?

After having a model, we can use method find() on the model of a particular collection to get documents of the collection. <query>: It is optional. It specifies a selection filter that is used to filter documents using various MongoDB query operators. If not passed, all the documents are returned.

What is the difference between id and _ID in Mongoose?

So, basically, the id getter returns a string representation of the document's _id (which is added to all MongoDB documents by default and have a default type of ObjectId ). Regarding what's better for referencing, that depends entirely on the context (i.e., do you want an ObjectId or a string ).


1 Answers

Since you're using mongoose-schema-extend, it seem like you could create a simple 'base' schema and extend your other schema's off that. If you want to search across all of them, use the base model.

For instance:

// base schema
var PersonSchema = new Schema({
  name : String
}, { 
  collection       : 'users', // everything will get saved in the same collection
  discriminatorKey : '_type' 
});

// two schema's that extend off it
var EmployeeSchema = PersonSchema.extend({ department : String });
var EmployerSchema = PersonSchema.extend({});

// materialize all three into models
var Person    = mongoose.model('Person',   PersonSchema);
var Employee  = mongoose.model('Employee', EmployeeSchema);
var Employer  = mongoose.model('Employer', EmployerSchema);

...

// create some people
new Employee({
  name       : 'Homer Simpson',
  department : 'Safety'
}).save(...);

new Employer({
  name : 'Charles Montgomery Burns',
}).save(...);

...

// search across employers and employees
Person.find({ ... }, function(err, people) {
  ...
});

However, I have to say that the advertised behaviour of find() returning the correct model instance according to the discriminator key doesn't work for me.

like image 198
robertklep Avatar answered Nov 15 '22 00:11

robertklep