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

Implementing MongoDB views #7855

Closed
BusbyActual opened this issue May 30, 2019 · 3 comments
Closed

Implementing MongoDB views #7855

BusbyActual opened this issue May 30, 2019 · 3 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@BusbyActual
Copy link

BusbyActual commented May 30, 2019

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.

     // OR mongoose.connection.db.createView
  mongoose.connection.db.createCollection('myViewNames', {  viewOn: 'runtime',  pipeline: [ 
    { 
      $group: {
       _id:  { channel: "$channel", connection: "$connection", deviceUniqueId: "$deviceUniqueId", month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, year: { $year: "$date" } },
       count: {
         $sum: 1
       },
       formula: {
         $avg: "formula"
       }
    }
   }])

module.exports.runtimeView = mongoose.model('myviewname', runtimeSchema);
// OR module.exports.runtimeView = mongoose.model('myViewName', runtimeSchema);

runtimeView.find().then((data) => {
     console.log(data, '%%');
});

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"

@BusbyActual
Copy link
Author

BusbyActual commented May 30, 2019

Since mongoose makes the views plural changing the target resolves the issue of creating the View
viewOn: 'runtime'
to
viewOn: 'runtimes'

@BusbyActual
Copy link
Author

BusbyActual commented May 31, 2019

I'm still at an impasse for the proper way to query the view.

 const runtimeSchema = new Schema({
  deviceUniqueId: { type: String }, 
  date: { type: Date, default: () => Date.now() },
  formula: { type: String, max: 100 },
  connection: { type: Number },
  channel: { type: Number }
}, { usePushEach: true });

module.exports.runtime = mongoose.model('runtime', runtimeSchema);
runtimeSchema.index({ 'date': 1, 'deviceUniqueId': 1, 'channel': 1, 'connection': 1 });

// View model for querying?              
module.exports.runtimeView = mongoose.model('runtime', runtimeSchema);
runtimeView.find().then((data) => {
   console.log(data, '%%');
});

@BusbyActual BusbyActual reopened this May 31, 2019
@vkarpov15 vkarpov15 modified the milestones: 5.5.14, 5.5.15 Jun 3, 2019
@vkarpov15
Copy link
Collaborator

In your last comment, runtimeView isn't defined when you do runtimeView.find().

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

@vkarpov15 vkarpov15 removed this from the 5.5.15 milestone Jun 12, 2019
@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Jun 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

2 participants