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
Implementing MongoDB views #7855
Comments
Since mongoose makes the views plural changing the target resolves the issue of creating the View |
I'm still at an impasse for the proper way to query the view.
|
In your last comment, You're also adding indexes to your schema after compiling your model. That won't work. Here's a working example of creating and querying a view with Mongoose: const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('debug', true);
const GITHUB_ISSUE = `gh7855`;
const connectionString = `mongodb://localhost:27017/${ GITHUB_ISSUE }`;
const { Schema } = mongoose;
run().then(() => console.log('done')).catch(error => console.error(error.stack));
async function run() {
await mongoose.connect(connectionString);
await mongoose.connection.dropDatabase();
const Model = mongoose.model('Test', new Schema({ name: String }));
await Model.create({ name: 'foo' });
await mongoose.connection.db.createCollection('testview', {
viewOn: 'tests',
pipeline: [
{ $group: { _id: '$name', count: { $sum: 1 } } }
]
});
const ViewModel = await mongoose.model('TestView', new Schema({ _id: String, count: Number }), 'testview');
console.log(await ViewModel.findOne());
} |
Do you want to request a feature or report a bug?
bug / clarification
What is the current behavior?
Mongoose appears to be creating a collection however it doesn't behave as a view. No data is returned from requests. I followed a related #5694 issue and follow this with little success.
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
Mongoose should create a view and when querying the view it should return the aggregate values.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
"Node": "8.2.1"
"Mongoose": "^5.5.11"
"MongoDB": "3.6.2"
The text was updated successfully, but these errors were encountered: