Category / Section
How to query MongoDB collections while connecting to Bold BI?
Published:
The direct querying option is not available in Bold BI for MongoDB data source. However, you can utilize views instead.
To create views in MongoDB, follow these steps:
- In a MongoDB client like MongoDB Compass, right-click on the collection in the connection tree where you want to add a view.
- Select the option “Add view here”. This action will open the view editor, allowing you to construct your query using the desired operators and add stages accordingly.
- Click on “Add stage” to introduce a new stage.
- From the dropdown menu, select the desired operator. Modify steps 3 and 4 as required.
- Once you have completed the query construction, click on “Save view” to save your changes.
To create a view from MongoDB CLI, we should use the db.createView() or db.createCollection() command, specifying the view name, the view source collection and the aggregation pipeline.
Syntax:
Create Collection with View:
db.createCollection("<viewname>",{
"viewOn" : "<source collection="" name="">",
"pipeline" : [<pipeline>],
"collation" : {<collation>}
})
Create View:
db.createView("<viewname>","<source collection="" name="">",
[<pipeline>],
{
"collation" : {<collation>}
})
Example:
db.students.insertMany( [
{ StudentID: 22001,year: 1,Branch: "CSE",score: 87 },
{ StudentID: 21001,year: 2,Branch: "ECE",score: 90 },
{ StudentID: 20010,year: 3,Branch: "CSE",score: 55 },
{ StudentID: 22021,year: 1,Branch: "CSE",score: 75 }
] )
db.createView("firstYears","students",
[ { $match: { year: 1,Branch: 1 } } ]
)
db.createCollection("firstYears",
{
viewOn: "students",
pipeline: [ { $match: { year: 1,Branch: 1 } } ]
})
db.firstYears.find({year: 1})
Output:
[
{ StudentID: 22001,year: 1,Branch: "CSE",score: 87 },
{ StudentID: 22021,year: 1,Branch: "CSE",score: 75 }
]
- Creating a view requires specifying a collection or a previous existing view.
- When a view is the source collection from another view, it allows us to execute a chained aggregation.
How to connect MongoDB views in Bold BI:
Once the Views are created you can find the views under the Collections dropdown in MongoDB data source. Select it and proceed to fetch the view data.