0

I have a collection in mongodb with nested data like this:

{ 
    "lat" : "-33.890542", 
    "lng" : "151.274856", 
    "city" : "San Jose", 
    "area" : "SJSU", 
    "sensordata" : [
        {
            "timestmp" : NumberLong(1414850400000), 
            "temp" : NumberInt(21)
        }, 
        {
            "timestmp" : NumberLong(1414850460000), 
            "temp" : NumberInt(12)
        }, 
        {
            "timestmp" : NumberLong(1414850520000), 
            "temp" : NumberInt(13)
        }, 
        {
            "timestmp" : NumberLong(1414850580000), 
            "temp" : NumberInt(15)
        } 

    ]
}

{ 
    "lat" : "-33.890542", 
    "lng" : "151.274856", 
    "city" : "San Jose", 
    "area" : "SJSU1", 
    "sensordata" : [
        {
            "timestmp" : NumberLong(1414850400000), 
            "temp" : NumberInt(21)
        }, 
        {
            "timestmp" : NumberLong(1414850460000), 
            "temp" : NumberInt(12)
        }, 
        {
            "timestmp" : NumberLong(1414850520000), 
            "temp" : NumberInt(13)
        }, 
        {
            "timestmp" : NumberLong(1414850580000), 
            "temp" : NumberInt(15)
        } 

    ]
}

What i want to do is fetch records based on certain condition of timestmp. eg: timestamp>1414850460000

So the entire record will be fetched but all the records under the Sensor data field will not be fetched. I am unable to formulate a query for this. Thanks

Himanshu Jain
  • 1,809
  • 1
  • 13
  • 23
  • That helps but I am still not able to fetch records using gt & lt. Here is my query: serverdb.asd.find({"sensordata.timestmp":{ $gt : 1414850400000, $lt : 1414850580000}}, { sensordata: { $elemMatch: { timestmp: { $gt : 1414850400000, $lt : 1414850580000}} } },function(err,docs){ console.log(docs); res.json(docs); }); Whats wrong in this. Fetches only 1 record with timestmp: 1414850460000 – Himanshu Jain Nov 25 '15 at 00:49
  • Am i wrong in thinking that $elemmatch will fetch only the first occurrence . I want all the docs inside the range – Himanshu Jain Nov 25 '15 at 01:04
  • Did you see this [answer](http://stackoverflow.com/a/24032549/3100115)? – styvane Nov 25 '15 at 01:13
  • Not what this question is about, but as a sidenote you really should change your `"lat"` and `"lng"` fields to a [supported storage format](https://docs.mongodb.org/manual/applications/geospatial-indexes/#location-data) for geospatial queries should you need it some day. By preference you really should use [GeoJSON](https://docs.mongodb.org/manual/reference/geojson/), and also be aware that the values you presently seem to have "imported" are "strings", when they really should be ( and need to be ) numeric types. – Blakes Seven Nov 25 '15 at 04:55
  • you can always use the aggregation framework for this. Like this: db.asd.aggregate({$unwind:'$sensordata'},{$match:{"sensordata.timestmp":{ $gt : 1414850400000, $lt : 1414850580000}}},{$group:{_id:'$_id',sensordata:{$push:'$sensordata'}}}) – Sarath Nair Nov 25 '15 at 05:55
  • modified the query using redact. why is it still not fetching any records. db.asd.aggregate([ { $match: { sensordata: { $elemMatch: { "timestmp": { $gt : 1414850400000, $lt : 1414850580000}} } }}, { $redact : { $cond: { if: { $and : [{ $gt: ["$timestmp",1414850400000] }, { $lt: ["$timestmp",1414850580000] }]}, then: "$$DESCEND", else: "$$PRUNE" } }}]); – Himanshu Jain Nov 25 '15 at 17:13

0 Answers0