0

I'm new in MongoDB, and I would like to know if there is a way to create a dynamic view inside MongoDB.

Let me be more precise: In mongo I have a collection with financial data, and an GUI interface which displays the data. But each users could reduce the data by adding or removing columns to the grid, and filter the grid: a classic use case.

What I would like to do, is to create a collection for each user that listens to the master table, according to the users filters: something like this:

mongo.createView(masterCollection, filters, mapReduce)

In this scenario, mongo updates each view, each time a modification is done in the master collection (update, delete, insert).

I could do something like this manually: create a table for a user, and use a tailable cursor on my master collection with the user filters and mapReduce, and the add, remove, or update the document in the user collection.

But, I have up to 100 simultaneous users, and it would open and keep alive 100 tailable cursors on the primary collection. I don't know enough mongo, but I think it's not a best practice to do something like this.

Actually I have a thread for each user that get data for the collection, according to the user filters, every 5 seconds.

Could you please let me now if there is a native mongo way to handle this scenario or a way to create a user view in mongo.

Thank you

1
  • As of version 3.2.6 MongoDB doesn't offer views on the database level. Commented Apr 29, 2016 at 13:11

2 Answers 2

3

Starting with MongoDB v3.4 (community and enterprise edition), there is a support for creating read only views from existing collections or other views. This feature utilises MongoDB aggregation pipeline.

To create a view from the mongo shell, you could do:

db.createView(<view>, <source>, <pipeline>, <collation> )

Using your scenario above, you could create:

db.createView(<"user_view_name">, <"source_collection_name">, <"aggregation pipeline operators">)

See all the available Aggregation Pipeline Operators - i.e. not only you could filter, you could also modify the document structure.

See MongoDB Views behaviour for more information.

Sign up to request clarification or add additional context in comments.

Comments

0

MongoDB Enterprise has a feature called 'Single View' which implements database views. It's more for aggregating data from multiple tables (e.g. parent/child relationships), and may be more than what you're looking for but is worth checking out. The downside, it's only available in the pricey Enterprise edition.

Check out the description here

2 Comments

Using the enterprise edition is not an option for me. I'll find an other way to do this. Nerver the less, thank Philipp and Hascut you for the answer
@zouroto The link provided here is to a typical use case for MongoDB ("building a single view of data") rather than a specific feature. This use case does not rely on the Enterprise version of MongoDB. MongoDB 3.4 added support for creating read-only views, which is a standard feature available in both Community and Enterprise editions of MongoDB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.