MongoDB: how to get the last values of two collections in a script?

64 Views Asked by At

I am using SpagoBI with MongoDB. like mentioned in the link [1] I can use script .js on my MongoDB data base to get results. What I need to do is to merge the two following queries results:

var query1=db['cygnus_/kurapath_enocean_switch2a_enocean'].find().sort({ recvTime : -1 }).limit(1) ;
var query2 = db['cygnus_/kurapath_enocean_switch2b_enocean'].find().sort({ recvTime : -1 }).limit(1);

If I put something like

var query= query1 + query2;

I got the error:

query has no method 'forEach' at serializeResult 

Any ideas how to do that? Thanks in advance for your help!

[1] http://wiki.spagobi.org/xwiki/bin/view/spagobi_server/data_set

1

There are 1 best solutions below

0
On BEST ANSWER

Try

var results = query1.toArray().concat(query2.toArray());

What happens here is that toArray() is used to convert the result cursors into standard Javascript arrays of documents (in this case arrays with just one entry because you used limit(1)). Then the standard Javascript array method concat is used to append one array to the other and store it in the new array results.

That array can then be used as you want.