Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
prateekraina
Memorable Member
Memorable Member

Extract data from Mongo DB using R

Hi,

 

I have connected my Power BI desktop with Mongo DB using R already.

I am able to see the data inside my collection.

 

Problem Statement

The data stored in Mongo has nested JSON array which is not readable to Power BI Directly. I have tried writing different type of query but i am not able to access the child elements of the data.

Sample Data

{

	"imageName": "5a6188cceaf68a74e499de66_humanDetection_1516342489874.jpg",
	"createdAt": "1516342487874",
	"resultCount": "1",
	"camId": "5a6188cceaf68a74e499de66",
	"result": [{
		"id": "0",
		"bboxes": {
			"y2": "736",
			"y1": "257",
			"x2": "961",
			"x1": "751"
		},
		"areaOfInterestId": "5a618ccc926866aa7464529a"
	}]
}


Query Tried:

= R.Execute("library(mongolite)
  m<-mongo(collection="""&Text.From(Collection)&""",db = """&Text.From(Database)&""",url=paste(""mongodb://"","""&Text.From(Username)&""","":"","""&Text.From(Password)&""",""@"","""&Text.From(Server)&""","":"","""&Text.From(Port)&""",""/"","""&Text.From(Database)&""",sep=''), verbose = FALSE)#(lf)Query<-m$find(query = ""{}"", fields = ""{\""_id\"":1,\""resultCount\"":1,\""resukt.bboxes.y2\"":1}"", sort = ""{}"", skip = 0, limit = 0, handler = NULL, pagesize = 1000)")



Kindly  help in accessing the elements inside bboxes in Result array.

 

Prateek Raina

@ImkeF: Need your help on this.

14 REPLIES 14
gilbertendaya
Helper IV
Helper IV

@prateekraina @ImkeF how are you able to connect to MongoDB using R? please help as I need to use the same set-up instead of ODBC. Thank you.

Anonymous
Not applicable

Hi @prateekraina,

 

  Did you get any solution? I am also facing the same issue.

 

Thanks,

Sud

Hi @Anonymous ,

 

No, unfortunately I was not able to get solution for this.

 

Regards,

Prateek Raina

WolfBiber
Microsoft Employee
Microsoft Employee

Hey,

try something like

let
    RSC = R.Execute("library(mongolite)  m<-mongo(collection="""&Text.From(Collection)&""",db = """&Text.From(Database)&""",url=paste(""mongodb://"","""&Text.From(Username)&""","":"","""&Text.From(Password)&""",""@"","""&Text.From(Server)&""","":"","""&Text.From(Port)&""",""/"","""&Text.From(Database)&""",sep=''), verbose = FALSE)#(lf)Query<-m$find(query = ""{}"", fields = ""{\""_id\"":1,\""resultCount\"":1,\""resukt.bboxes.y2\"":1}"", sort = ""{}"", skip = 0, limit = 0, handler = NULL, pagesize = 1000)") ,
   Source = Json.Document(RSC)
in
    Source

in Query Editor (View-->Advanced Editor)

@WolfBiber

 

Thanks for responding but this is not working. I tried below as you mentioned:

let
    RSC = R.Execute("library(mongolite)
  m<-mongo(collection="""&Text.From(Collection)&""",db = """&Text.From(Database)&""",url=paste(""mongodb://"","""&Text.From(Username)&""","":"","""&Text.From(Password)&""",""@"","""&Text.From(Server)&""","":"","""&Text.From(Port)&""",""/"","""&Text.From(Database)&""",sep=''), verbose = FALSE)#(lf)Query<-m$find(query = ""{}"", fields = ""{\""_id\"":1,\""resultCount\"":1,\""result.bboxes.y2\"":1}"", sort = ""{}"", skip = 0, limit = 0, handler = NULL, pagesize = 1000)"),
    Source = Json.Document(RSC)
in
    Source

 Step 1 is successful.

Solution1.PNG

However, step 2 is throwing below error:
Expression.Error: The parameter is expected to be of type Text.Type or Binary.Type

Solution2.PNG

Prateek Raina

Hi,

you have to use your result column (with the JSON data) in 

 Source = Json.Document(RSC.result)

But you have to look for the right syntax, cause I currently have no test data.

Hi @WolfBiber,

 

Is it not possible for you to look at the Step 1 snapshot and figure out the syntax based on the columns which are returned?

 

Prateek Raina

Hi @prateekraina,

so you want to extract the JSON-record in R an return the content of the sub-record bboxes as a table to Power BI?

This script works for me (with a link to the local json:

 

library(jsonlite)
json <- jsonlite::fromJSON("C:..Filepath/JsonMongoPrateek.json")
out <- json$result$bboxes

You can navigate with "$" through the nodes.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi @ImkeF,

 

It's great to hear from you. Actually, i need to use Mongolite as i am fetching the data from Mongo DB.

I would request you to go through the problem statement once again.

 

Prateek Raina

All I can recommend is to extract the full JSON from Mongo to R via the R-script in the query editor.

Once you have the JSON in R, you can use parts of my script to extract the inner parts there.

I have no clue how to do the extraction on MongoDB-level.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi @ImkeF,

 

The main issue is how to extract the full JSON from Mongo to R.
Using Mongolite i have not seen anywhere on internet of someone had tried to access a nested json array.

Can you please tag any expert who might have answer to this?

 

Prateek Raina

Sorry, but I don't know who is using this db.

I'd suggest to post your question on stackoverflow in the R-section (or Mongo).

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Ok @ImkeF. Thanks for your time.

Anonymous
Not applicable

Hi@prateekraina.

Facing the same issue did you get any solution for this and also can you explain how you connected to MongoDB using R in power bi

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.