How to create "View" in mongodb like MySql View?

how to create “View” in mongodb like MySql View?

There is no such thing in Mongo.
But when you are querying, you can filter fields.

see: http://docs.meteor.com/#/full/fieldspecifiers

have any package to do this?

There are views in Mongo now, since version 3.4. Here is how to create a view in Meteor:

// 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' }
  ],
});

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

Hi guys, did anyone use Mongo Views 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!

Did you ensure that each document created with the view pipeline has an _id specified? Minimongo won’t behave well without this.

Yes, @robfallows, each doc in aggregation results has _id specified.

Also check that _id is in the Random.id() format, I had an issue with that some time ago.

Thank you, @pmogollon, will check.

My concern is an additional event(s) that is(are) fired after initial data is published to UI… We have 2 ‘lookup’ stages in our pipeline for the View… and it looks like those additional updates are correlated to those stages. I.e. when we call View.find and observe changes, I see ‘added’ that is, essentially, results of first lookup in View’s aggregation… and then I see ‘changed’ triggered that essentially return results of second ‘lookup’ in View’s pipeline. Is that a normal behavior?