Problem source：https://groups.google.com/forum/#!topic/mongodb-user/Rjv6lyUCe1s .
Collection last3 has two fields: variable and timestamp. You need to first group documents by variable and find from each group the top 3 ones with the latest timestamp, and then find from the three documents the one with the earliest timestamp.
Below is the selection from last3:
A1: Connect to MongoDB. The connection string format is mongo://ip:port/db?arg=value&…
A2: Use find function to retrieve data from MongoDB, sort it and create a cursor. last3 is the collection name; no filtering criterion is specified; and all fields except _id will be retrieved and sorted by variable. In esProc find function, which is analogous to the combination of MongoDB find, sort and limit function, the filtering criterion syntax follows the MongoDB rules.
A3: Fetch data from the cursor by loop, getting a group of documents with the same variable field each time. A3’s working range is the indented B3 to B4, where A3 can be used to reference the loop variable. A3’s result is in-memory data. The following is one of the results of data fetching:
B3: Find from the current group of documents the three ones with the latest timestamp.
B4: Append each of B3’s loop results to B2. The result of B2 is as follows:
A5：Find the document with the earliest timestamp from B2. It is as follows:
A6：Close MongoDB connection.