• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to make mongodb find query using json

 
Dinesh Kumar Ramakrishnan
Ranch Hand
Posts: 68
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi to all,



i am new to mongodb. i'm trying to make query on mongolab database using REST api call my collection look like this


i want to retrieve documents where dept ="mca" as per MongoLab api i am trying to make query in following following manner with "q" parameter


https://api.mongolab.com/api/1/databases/mydb/collections/mycollection?q={%22dept%22:%22mca%22}&apiKey=myapikey

please anyone tell how to make json query using find() method it will help to understand better.

 
arumugarani sundaram
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Mongo the querying part is kind of very easy.


I am not very sure how your json document is structured. You have given in the sampe "dept" param inside "stu" and "stu1". Is it valid? Suppose if you have 10 students, will you have stu1,stu2... not sure about json doc..

suppose your json doc is

{"_id":<MongoId>
"stu":{
"dept": "mca",
"place":"chennai"
}
}

Then another doc with

{"_id":<MongoId>
"stu":{
"dept": "mba",
"place":"chennai"
}
}

.....



{"_id":<MongoId>
"stu":{
"dept": "mca",
"place":"chennai"
}
}


Then your query param dept is inside "stu" object

So in mongo shell, you can query like

db.<collectionname>.find({"stu.dept":"mca"})


Hope this helps
 
Dinesh Kumar Ramakrishnan
Ranch Hand
Posts: 68
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you @arumugarani sundaram i changed doc structure it work's
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic