Skip to content

Instantly share code, notes, and snippets.

@reggi
Created August 30, 2012 16:14
Show Gist options
  • Save reggi/3532063 to your computer and use it in GitHub Desktop.
Save reggi/3532063 to your computer and use it in GitHub Desktop.
Mongo
{
"transactions": [
{
"amount": "10.00",
"authorization": null,
"created_at": "2012-08-24T14:02:50-04:00",
"gateway": "bogus",
"id": 999225661,
"kind": "capture",
"order_id": 450789469,
"status": "success",
"test": true,
"receipt": {}
}
]
}
{
"transactions": [
{
"amount": "10.00",
"authorization": null,
"created_at": "2012-08-24T14:02:50-04:00",
"gateway": "bogus",
"id": 999225661,
"kind": "sale",
"order_id": 450789469,
"status": "success",
"test": true,
"receipt": {}
}
]
}
@reggi
Copy link
Author

reggi commented Aug 30, 2012

I am trying to query dates embeded inside of a array, the date that I want is defined by other values in the array mainly kind=sale || kind=capture && status=success can I process which array created_at I want using mongo / javascript or do I have to include the field on insert?

@andredublin
Copy link

so I would do
for (i in obj) { // obj being your returned json
if (obj[i].kind === 'sale') {
console.log('got it');
}
}

am i on the right track?

@reggi
Copy link
Author

reggi commented Aug 30, 2012

This would be ideal when it comes to manipulating an order and adding a order.transaction next to the order.transactions. One being the new Date() object that I can sort by, and the other being a array.

var key;
var(key in order.transactions){
    var value = orders[key];
    if(value.kind === "sale" || value.kind === "capture" && value.status === "success"){
        order.transaction = {
            "created_at":value.created_at,
        }
    }
}

But I am interested on not inserting this into the database, I would like to not have a fixed order.transaction field, and much rather generate it on-the-fly. is that possible?

@andredublin
Copy link

You should be able to do this. You must initialize that object property before you can work on it. If the transactions are coming in as an array of objects then you will have to loop through like so

    order.transaction = [];

    for (i = 0; i < order.transactions.length; i++) {
        var value = order.transactions[i];
        if (value.kind === 'sale' || value.kind === 'capture' && value.kind === 'success') {
            order.transaction[i] = {
                "created_at": value.created_at
            }
        }
    }

    console.log(order);
```javascript

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment