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

Is there a way we can handle views with mongoose !? #5694

Closed
muhammadfaizan opened this issue Oct 6, 2017 · 6 comments · Fixed by #12526
Closed

Is there a way we can handle views with mongoose !? #5694

muhammadfaizan opened this issue Oct 6, 2017 · 6 comments · Fixed by #12526
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@muhammadfaizan
Copy link

feature

What is the current behavior?
currently there is a plugin, but that only selects few fields, and doesn't work on multiple models

What is the expected behavior?
https://docs.mongodb.com/manual/reference/method/db.createView/#db.createView

using mongoDB 3.4

@vkarpov15
Copy link
Collaborator

Not sure what you mean by handling views. What's the plugin you're talking about?

Once a view is created, as far as mongoose is concerned it is just a read-only collection, so as long as you have a schema in mongoose you can query the view as you would any normal collection.

@vkarpov15 vkarpov15 added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Oct 7, 2017
@muhammadfaizan
Copy link
Author

I meant how can i create read only collection with mongoose, that is allowed by mongodb

@vkarpov15
Copy link
Collaborator

MyModel.connection.db.createCollection('myViewName', { viewOn: 'existingCollection', pipeline: [/* aggregation pipeline here */] }); http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html#createCollection

@alzalabany
Copy link

so we can use createCollection to create a view. and redirect our model to use that view ?
im trying to implement something like soft delete where i have 2 collections 1_ all_data & 2nd active_only . and i want to from now on only use active only every where in app to hide deleted docs. but i cannt wrap my head how to do so with mongoose

@vkarpov15
Copy link
Collaborator

@alzalabany you would create 2 separate models, one that points to the allData collection and one that points to the activeData view. Use the third param to mongoose.model to specify the collection name http://mongoosejs.com/docs/api.html#mongoose_Mongoose-model

@fullStackDataSolutions
Copy link

It would be great if there was some kind of code example of this.

@vkarpov15 vkarpov15 reopened this Sep 26, 2022
@vkarpov15 vkarpov15 added this to the 6.6.3 milestone Sep 26, 2022
@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity labels Sep 26, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.6.3, 6.6.4, 6.6.5 Sep 26, 2022
vkarpov15 added a commit that referenced this issue Oct 5, 2022
vkarpov15 added a commit that referenced this issue Oct 5, 2022
docs(models): add section on MongoDB Views
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants