Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose Querying Views

Tags:

I'm currently using mongoose v. 5.25, against mongoDB v.3.6.

My application is supposed to query data from many different views, for instance, a view I currently have at my DB: db.joboffers_view.find()

will return many records that have been aggregated from different collections.

For a normal collection model, I query it like so:

 const model = db.model(attribute);
 /*where attribute, can be any registered schema */
 model.find().
       then((result) => {
           resolve(result);
       }).
       catch((err) => {
           reject(err);
       });

Then way I register my models is something like this (simplified code):

//...
//abstracting boring connection methods
const db = mongoose.connection
//...

//simple model schema
const users_schema = {
   _id: ObjectId,
   another_field: String
};

//here I'm registering a schema for a VIEW, instead of normal collection
const view_schema = {
   _id: ObjectId,
   another_field: String
};
//...
//then

db.model('users', users_schema);
db.model('view', view_schema);

When I run a query from any of my registered models, I get the results just fine. However, when I run it against a model that represents a view on my mongo database, it returns an empty array.

No errors, no nothing, just an empty array.

I have looked through mongoose documentation, and I didn't find any specific method or pattern for querying a view, instead of a collection data.

It seems to be the same way I would do for any other collection I have in my system.

Am I missing something?

like image 391
Former User of Mine Avatar asked Jul 26 '18 18:07

Former User of Mine


People also ask

Can we have views in MongoDB?

MongoDB provides two different view types: standard views and on-demand materialized views. Both view types return the results from an aggregation pipeline. Standard views are computed when you read the view, and are not stored to disk. On-demand materialized views are stored on and read from disk.

What does findById return in mongoose?

Return value findById returns the document where the _id field matches the specified id . If the document is not found, the function returns null .

What is $GTE in mongoose?

$gte selects the documents where the value of the field is greater than or equal to (i.e. >= ) a specified value (e.g. value .)

How do I view tables in MongoDB?

If you want to check your databases list, use the command show dbs. Your created database (mydb) is not present in list. To display database, you need to insert at least one document into it. In MongoDB default database is test.


1 Answers

I also faced the same issue and figured out the problem is that mongoose, by default, reads collection names by pluralizing the model/view name.

So when you create any view and want to use it in mongoose, either make sure your view name is plural (add s to end of view name) or pass a collection name when initializing a schema.

Example

const users_schema = {
   _id: ObjectId,
   another_field: String
};
mongoose.model('vw_user_info', users_schema, 'vw_user_info');
like image 106
Om Prakash Sharma Avatar answered Sep 28 '22 17:09

Om Prakash Sharma