Solved

Mongodb using last result as the next query input?

Posted on 2016-10-24
6
50 Views
Last Modified: 2016-11-06
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?  


below are the mongodb documents sample.

{
    "_id" : ObjectId("580e1f26fa9382133c202a04"),
    "ProcessTime" : "24472016-22:47:58",
    "HostName" : "APPServer",
    "IPAddress" : "192.168.0.1",
    "OS" : " Linux",
    "Status" : "Machine running",
    "CurrentTicks" : "636129460749210826"
},
{
    "_id" : ObjectId("580e1f26fa9382133c202a05"),
    "ProcessTime" : "24472016-22:47:58",
    "HostName" : "APPServer2",
    "IPAddress" : "192.168.0.2",
    "OS" : " Linux",
    "Status" : "Machine running",
    "CurrentTicks" : "636129460749210826"
},
{
    "_id" : ObjectId("580e21e1fa9382133c202d16"),
    "ProcessTime" : "24582016-22:58:57",
    "HostName" : "DC",
    "IPAddress" : "192.168.0.43",
    "OS" : " Windows",
    "Status" : "Machine running",
    "CurrentTicks" : "636129467352901129"
},
{
    "_id" : ObjectId("580e21e1fa9382133c202d17"),
    "ProcessTime" : "24582016-22:58:57",
    "HostName" : "DC2",
    "IPAddress" : "192.168.0.45",
    "OS" : " Windows",
    "Status" : "Machine running",
    "CurrentTicks" : "636129467352901129"
}

Open in new window

Result_output_1.JPG
0
Comment
Question by:Steve Wong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 41857780
Can you not just do a sort and take the top item?

If you don't find a solution - bump this thread and I will see if I can put a script together.
0
 

Author Comment

by:Steve Wong
ID: 41859000
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?

Regards,
Steve
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 41875673
1. how to retrieve the Tick's value from LastTick
with nodejs :
var tick = LastTick.Tick;

Open in new window

2. will my second Query success if I run in that way?
Not sure what it going to return as your CurrentTicks is not unique

3. if not what will be better way to do it?  
Another way to  get the last record using the _id :
var LastTick = db.HKMachineStatus_v2.find({}).sort({_id:-1}).limit(1);

Open in new window

0
 

Author Closing Comment

by:Steve Wong
ID: 41875897
Thanks, it works great
0
 
LVL 82

Expert Comment

by:leakim971
ID: 41876055
why a B grade ?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Add - in a loop to first and last element. 6 59
Woo-commerce gallery not working properly 12 39
Slow Down an Animation 3 28
Sending JSON Via POST In PHP 7 21
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question