rajeshkumarprk
rajeshkumarprk

Reputation: 49

Time series data storage in mongo DB using PHP

I am working on a web services using MongoDB. What I want to do is have to store date like below using PHP

Initially a collection look like below (insert)

{
    timestamp_date: ISODate("2013-10-10"),
    type: “memory_used”,
    values: {
        0: { userid: 1, lat: 12.121212, long : 11.111111, timestamp: 2014-02-12 06:50:30 },            
      }
     }

I have to update the above record look like below (update)

 {
  timestamp_date: ISODate("2013-10-10"),
  type: “memory_used”,
  values: {
    0: { userid: 1,  lat: 12.121212, long : 11.111111, timestamp: 2014-02-12 06:50:30 },
    1: { userid: 1,  lat: 14.141414, long : 10.101010, timestamp: 2014-02-12 07:00:30 },
    .
    .
    .
    99:{ userid: 1,  lat: 15.151515, long : 12.121212, timestamp: 2014-02-12 12:50:30 },
    .
    .
    .


  }
 }

and how to find the latest record(current location of the user). Can anyone help me using PHP & MongoDB code?

Thanks

Upvotes: 0

Views: 461

Answers (1)

heinob
heinob

Reputation: 19474

The first thing you have to do is to convert your values object into an array:

values: [
    { userid: 1,  lat: 12.121212, long : 11.111111, timestamp: 2014-02-12 06:50:30 },
    { userid: 1,  lat: 14.141414, long : 10.101010, timestamp: 2014-02-12 07:00:30 },
    ...
]

Then you can find the last location of each user by using aggregate, $unwind, $group and $last.

Upvotes: 1

Related Questions