How to create "View" on mongo from Meteor?

I use Mongo 3.4.
Could I create View from Meteor App?

// Create on mongo shell
db.createView (
   "orderView",
   "order",
   [
     {
         $lookup: {
                from: "customer",
                localField: "customerId",
                foreignField: "_id",
                as: "customerDoc"
              }
     },
     {
         $unwind: "$customerDoc"
     }
   ]
)

Please help me.

Meteor doesn’t use Mongo views. Basically you only use find(). You instead create your views in Blaze or React, using template helpers and such. Download the demo apps and have a look at how they’re made.

thanks, could I execute mongo syntax directly on Meteor?

Yeah, all the basic stuff is supported at least. Both on the server and client (browser) as well.

could I run mongo command on meteor

db.createView (
   "orderView",
   "order",
   [
     {
         $lookup: {
                from: "customer",
                localField: "customerId",
                foreignField: "_id",
                as: "customerDoc"
              }
     },
     {
         $unwind: "$customerDoc"
     }
   ]
)
2 Likes

Any news on this feature? we need to run everything on meteor server side upon installation.

I need this option too :sunny:

Have you tried running your command using rawCollection https://docs.meteor.com/api/collections.html#Mongo-Collection-rawCollection? You can use this inside a Meteor method.

1 Like

rawCollection is useful for running commands on a collection that already exists. Views, however, are computed on demand during read operations and have to be created in advance. The syntax is db.createView(<view>, <source>, <pipeline>, <collation>). Alternatively, a view can be created using db.createCollection.

@theara and @cagapejuvy, you can create views within Meteor like so:

// Use a collection that already exists that you're already using
const SomeCollection = new Mongo.Collection('some_collection');

// Then grab the raw db handler
// @link http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html
const db = SomeCollection.rawDatabase();

// Unfortunately, this driver doesn't support the `db.createView` method
// So let's use `db.createCollection` instead
// @link https://docs.mongodb.com/manual/reference/method/db.createCollection/#db.createCollection
db.createCollection('orderView', { // name of the view to create
  viewOn: 'order', // name of source collection from which to create the view
  pipeline: [{
    $lookup: {
      from: 'customer',
      localField: 'customerId',
      foreignField: '_id',
      as: 'customerDoc',
    } }, { $unwind: '$customerDoc' }
  ],
});
2 Likes

Thanks @micchickenburger, now I base on Meteor 1.7 (Mongo 3.6, Mongo Driver Node 3.X).

Hi guys, did anyone use Mongo View in Subscriptions?

We query Mongo View in publication and use .observeChanges() to publish results. For some reason, after 3-5 seconds all the documents got removed from minimongo as result of ‘removed’ event fired. Did anyone experience anything similar?

Thank you!

Can you please let me know how do you publish such a view and use it from the client side ?