Mongodb using last result as the next query input?
I have a mongodb document as below. I want to return the documents with Maximum CurrentTicks, but since the tick was dynamically generated, I would need to return it to a variable LastTick, but when I try to retireve it in to the next query
First Query: var LastTick = db.HKMachineStatus_v2.aggregate({$group: {_id : '' , Tick: {$max: "$CurrentTicks"}}})
Second Query: db.HKMachineStatus_v2.find({"CurrentTicks" : LastTick})
it just return me "Fetched 0 record(s) in 5ms" But there is no result to return.
Then I only run the first Query it did return me the result as the image attached Result_outPut_1.jpg .
So my questions are
1. how to retrieve the Tick's value from LastTick
2. will my second Query success if I run in that way?
3. if not what will be better way to do it?
If you don't find a solution - bump this thread and I will see if I can put a script together.
Steve Wong
ASKER
Hi Julian,
Thanks for your reply, I have some background to share with you first.
Background:
Currently I had 75 Machines provisioned, two days ago I scanned there was a 33 machines provisioned but last week there was 150+ machine provisioned.
I have a script running at the server backend to gather all the machines current status, I didn't know where they were (which subnet and what there name was/is ) and how many we had, machines were dynamically provision and dismissal.
I knew the data-set is bounded with a Ticker. Each batch run on the script are on the same ticker no.
So could you kindly show me some scripts on how to make a mongodb query to get all machines from the largest ticker?
If you don't find a solution - bump this thread and I will see if I can put a script together.