Bitcoin Forum
June 08, 2024, 08:17:47 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: [CLOSED] ANSWER SIMPLE MONGODB QUESTION: GET 0.05 BITCOINS :) [CLOSED]  (Read 449 times)
Lucky7Gaming (OP)
Member
**
Offline Offline

Activity: 87
Merit: 10


View Profile
July 04, 2016, 11:17:24 PM
Last edit: July 05, 2016, 02:05:49 AM by Lucky7Gaming
 #1

So my friend has stuff posting in a mongo database like this:
Code:
{
    "_id" : "device01",
    "event" : [
            {
                    "temperature" : "32.00",
                    "when" : ISODate("2016-07-02T00:21:41.441Z")
            },
            {
                    "temperature" : "42.00",
                    "when" : ISODate("2016-07-02T00:21:46.766Z")
            },            
    ]
}
 {
    "_id" : "device02",
    "event" : [
            {
                    "temperature" : "22.00",
                    "when" : ISODate("2016-06-02T00:21:41.441Z")
            },
            {
                    "temperature" : "43.00",
                    "when" : ISODate("2016-07-02T00:21:46.766Z")
            },            
    ]
}

and he wants to be able to query one of the devices on command and have an array return of the temperature, or when  (query device2 temp and get [22.00, 43.00] returned etc.  )

I tried this:
Code:
db.collection('test_mqtt').aggregate([
    {$unwind: '$event'},
    {$group: {
        _id: '$_id',
        t: {$push: '$event.temperature'}
        d: {$push: '$event.when'}
    }}
]);
but its somehow not working....its pulling my hair out, and driving me crazy.  Using Node, if anyone can figure it out, then send the code and post the address for 0.05 bitcoins!!!! Smiley


Earn BTC investing in Peer to Peer Loans on BTCJam.
http://tinyurl.com/lc4wmvx
grim007
Sr. Member
****
Offline Offline

Activity: 378
Merit: 250



View Profile
July 05, 2016, 01:00:44 AM
 #2

it is not simple chief. it needs more patience and knowledge of course. i will try it. challenge accepted
Lucky7Gaming (OP)
Member
**
Offline Offline

Activity: 87
Merit: 10


View Profile
July 05, 2016, 01:01:19 AM
 #3

it is not simple chief. it needs more patience and knowledge of course. i will try it. challenge accepted

Haha alright, I've been working on this for almost a day now and can't find the solution...... but I'm also a sh*t high schooler programmer

Earn BTC investing in Peer to Peer Loans on BTCJam.
http://tinyurl.com/lc4wmvx
Lavander
Full Member
***
Offline Offline

Activity: 369
Merit: 101


View Profile
July 05, 2016, 01:06:51 AM
 #4

good rate for one hour of work Wink I can't promise anything, but i will look in to that.. I think i know where you stuck Wink
Lucky7Gaming (OP)
Member
**
Offline Offline

Activity: 87
Merit: 10


View Profile
July 05, 2016, 01:10:49 AM
 #5

If this helps any, I also tried

Code:
 var resultArray=[];
    mongo.connect(url, function(err, db) {
    var cursor = db.collection('test_mqtt').find({"_id":"arduino01"},{"event.value":1,"_id":0});
         cursor.forEach(function(doc, err) {
         resultArray.push(doc.event);
}, function() {
  db.close();
  console.log(resultArray);
 
});

but the array isn't right.   I need to be able to grab (for only the device I want) a plain array of temperature values and a plain array for time values for a specified sensor.......and that's in some weird object format.   

Earn BTC investing in Peer to Peer Loans on BTCJam.
http://tinyurl.com/lc4wmvx
Lavander
Full Member
***
Offline Offline

Activity: 369
Merit: 101


View Profile
July 05, 2016, 01:15:37 AM
 #6

If this helps any, I also tried

Code:
 var resultArray=[];
    mongo.connect(url, function(err, db) {
    var cursor = db.collection('test_mqtt').find({"_id":"arduino01"},{"event.value":1,"_id":0});
         cursor.forEach(function(doc, err) {
         resultArray.push(doc.event);
}, function() {
  db.close();
  console.log(resultArray);
 
});

but the array isn't right.   I need to be able to grab (for only the device I want) a plain array of temperature values and a plain array for time values for a specified sensor.......and that's in some weird object format.   

and here i stuck as well.. sorry dude, but i thought it will be easiest. It looks like i'm out for tonight. I hope @grimo007 will find out what is going on Wink

Good luck guys!
Lucky7Gaming (OP)
Member
**
Offline Offline

Activity: 87
Merit: 10


View Profile
July 05, 2016, 01:34:40 AM
 #7

If this helps any, I also tried

Code:
 var resultArray=[];
    mongo.connect(url, function(err, db) {
    var cursor = db.collection('test_mqtt').find({"_id":"arduino01"},{"event.value":1,"_id":0});
         cursor.forEach(function(doc, err) {
         resultArray.push(doc.event);
}, function() {
  db.close();
  console.log(resultArray);
 
});

but the array isn't right.   I need to be able to grab (for only the device I want) a plain array of temperature values and a plain array for time values for a specified sensor.......and that's in some weird object format.   

and here i stuck as well.. sorry dude, but i thought it will be easiest. It looks like i'm out for tonight. I hope @grimo007 will find out what is going on Wink

Good luck guys!

Alright, thanks for trying to help Smiley. Maybe this is impossible, I can change the way the data is saved if there's any recommendations on that XD

Earn BTC investing in Peer to Peer Loans on BTCJam.
http://tinyurl.com/lc4wmvx
Lucky7Gaming (OP)
Member
**
Offline Offline

Activity: 87
Merit: 10


View Profile
July 05, 2016, 01:38:44 AM
 #8

I've been trying to save the data using,  
Code:
collection.update(  
  { _id:key },
  { $push:  { event: { value:String(payload), when:new Date() } }  },
  { upsert:true },
but maybe that's not the right way to go?  maybe some sort of nonformatted list is better............

changing it to not have events does give a better format I think

Code:
        "_id" : "device01",
        "value" : [
                "36.00",
                "23.60"
        ],
        "when" : [
                ISODate("2016-07-05T01:42:38.314Z"),
                ISODate("2016-07-05T01:42:43.631Z")
        ]

Earn BTC investing in Peer to Peer Loans on BTCJam.
http://tinyurl.com/lc4wmvx
Lucky7Gaming (OP)
Member
**
Offline Offline

Activity: 87
Merit: 10


View Profile
July 05, 2016, 02:05:20 AM
 #9

Alright, problem solved in a sketchy way.  Data is inputted this way:

Code:
function insertEvent(topic,payload) {  
  var key=topic.replace(deviceRoot,'');
  console.log(payload);
  collection.update( 
  { _id:key },
  { $push:   { value:String(payload), when:new Date()  }  },
  { upsert:true },
  function(err,docs) {
    if(err) { console.log("Insert fail"); } // Improve error handling
  }
  )
}

which gives
Code:
   "_id" : device01
   "temp" :[ 33.2,
34

]
   "when" : [
                ISODate("2016-07-05T01:42:38.314Z"),
                ISODate("2016-07-05T01:42:43.631Z"),
                ISODate("2016-07-05T01:42:48.964Z"),
}

which can be used sketchily with
Code:
 var resultArray=[];
    mongo.connect(url, function(err, db) {
    var cursor = db.collection('test_mqtt').find({"_id":"arduino01"},{"value":1,"_id":0});
         cursor.forEach(function(doc, err) {
         resultArray.push(doc.value);
}, function() {
  db.close();
  console.log(resultArray[0]);


but Lavender, I'll tip for pointing me in the right direction by saying it was impossible Smiley  PM me your address (apparently btc is burning a hole in my hdd)

Earn BTC investing in Peer to Peer Loans on BTCJam.
http://tinyurl.com/lc4wmvx
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!