0

I have built an application that stores all of my data in a MongoDB with thousands of entries. Now I want to query it from an HTML page. I have been able to successfully load the data with a button click, but I am unsure as to how to write a query for it. In standard MySQL, I could do something like "SELECT * from dbname where initials = "AA" and sector = "ECHO" " which are fields in the database. I have these fields in my object as well. How would I write the query to achieve the results similar to the MySQL statement above?

This is a sample of the array of objects data being returned:

[{_id: {$oid: "58306ce77df6dc1268c87142"}, cjs: "ZV", comment: null, current: false, duration: 50,…},…]
[0 … 99]
0:{_id: {$oid: "58306ce77df6dc1268c87142"}, cjs: "ZV", comment: null, current: false, duration: 50,…}
_id:{$oid: "58306ce77df6dc1268c87142"}
cjs:"ZV"
comment:null
current:false
duration:50
duration_string:null
error:null
expires_at:"2021-10-04T00:41:34.000Z"
fde_id_final:"778152728"
fde_id_initial:"778134619"
function:"SAT.IN"
initials:"AA"
json_class:"SatTransaction"
name:"Al Albert"
ot:false
position:"RDR"
quality:null
satin:"2016-10-05T00:41:34.000Z"
satout:"2016-10-05T01:32:20.000Z"
sector:"ECHO"
shift_date:"2016-10-04T00:00:00.000Z"
shift_duration:508
shift_duration_string:null
shift_end:58
shift_start:990
trainee:false
1:{_id: {$oid: "58306ce77df6dc1268c87144"}, cjs: "FY", comment: null, current: false, duration: 51,…}

And this is my basic HTML where I use a button to load the entire JSON file:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>


$(document).ready(function(){
    $("button").click(function(){
        $.getJSON("http://maccdx170131:4567/api/v1/sat", function(result){

        });
    });
});





</script>
</head>
<body>

<button>Get JSON data</button>

<div></div>

</body>
</html>

Thanks for any help or advice!!

user2843365
  • 377
  • 2
  • 7
  • 20
  • 1
    Possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Teemu Jan 29 '17 at 13:51
  • What you've quoted is not JSON. In JSON, property names must be in double quotes for a start. – T.J. Crowder Jan 29 '17 at 13:54
  • You are correct - it's not JSON. It's an array of objects. I was getting two of my projects mixed up. Sorry for the confusion – user2843365 Jan 29 '17 at 13:59
  • One normally does queries against the database rather than loading data into memory and trying to do filtering there. MongoDB has query facilities for doing this. You could start with [this](https://docs.mongodb.com/manual/tutorial/query-documents/) – rasmeister Jan 29 '17 at 14:01

2 Answers2

1

I would create a RESTful api that can handle queries. These can either be included in a header or as query parameters off the end of your url:

api/v1/sat?dataItem1=12 the query would have to be encoded (shown here without that for ease of illustration)

Then I would do mongoDB queries from your server code and return that to the client as indicated in other answers here.

rasmeister
  • 1,986
  • 1
  • 13
  • 19
0

To perform the query on the server you can use MongoDB's $and query selector:

collection.find({
  $and: [
    {'initials': 'AA'},
    {'sector': 'ECHO'}
  ]
})

Obviously you would have to pass the parameters in your AJAX request if the query is to be dynamic.

Dan Nagle
  • 4,384
  • 1
  • 16
  • 28