是否有可能在node.js中获得二维数组?

时间:2016-05-17 06:47:08

标签: node.js mongodb

在node.js中,我正在从两个集合(momgoDB)中检索值,我想将它们存储到二维数组中。这可能吗?如果是,那么请帮助我。

的node.js

router.post('/manage-product', function(req, res){
    vr1 = req.body.code1;
    vr = req.body.code;
    var um = ObjectId(req.body.userId);
    var findProducts = function(db, callback) {
        var cursor =db.collection('proInfo').find({userId:um, purchased:{$ne:vr}, contributed:{$ne:vr1}}).toArray(function(err, docs){
            if(err){
                callback(new Error("Some problem"));
            }else{
                callback(null,docs);
            } 
        });
    };

    MongoClient.connect(url, function(err, db) {
        assert.equal(null, err);
        findProducts(db, function(err,docs) {
            db.close();
            console.log(docs);

            var findPurchaserInfo = function(db, callback) {
                 var owner_id = um;
                 var cursor =db.collection('purchased').find({ownerId:um.toString().trim()}).toArray(function(err, docs1){
                    if(err){
                        callback(new Error("Some problem"));
                    } else {
                        callback(null,docs1);
                    } 
                });
            };  
            MongoClient.connect(url, function(err, db) {
                assert.equal(null, err);
                findPurchaserInfo (db, function(err,docs1) {
                    db.close();
                    console.log(docs1);
                });
            }); 
        });
    });     
});

第一个集合(proInfo)值存储在docs中,第二个集合(purchased)值存储在docs1中。现在我想将它们存储在一个数组中。我认为这是二维数组。在docs1我得到了 [ { _id: 5733006ace25b9682abb7c89, quantity: '1', store: 'amazon', purchasedPerson: 'vinay', email: 'vinay@gmail.com', message: 'I purchased this item for you', terms: 'yes', purchasedItemID: '5732c8e6599bfcc031013925', purchasedDate: '2016-05-11T09:50:17.718Z', ownerId: '56fe44836ce2226431f5388f' }, { _id: 5733397f3e102e40365bb5de, quantity: '1', store: 'amazon', purchasedPerson: 'Admin', email: 'admin@gmail.com', message: 'I purchased this item for you', terms: 'yes', purchasedItemID: '5732c95d599bfcc031013929', purchasedDate: '2016-05-11T13:53:45.606Z', ownerId: '56fe44836ce2226431f5388f' } ]

and in `docs` i am getting 

`[ { _id: 5732c95d599bfcc031013929,
Product_Name: 'Home City Gloria Three Seater Sectional Sofa (Brown)',
Brand: 'Home In The City',
Color: '',
Image: 'http://ecx.images-amazon.com/images/I/31zClnoqO0L._SX300_QL70_.jpg',

Price: '28,279.00',
Rating: '',
Description: '',
Category: 'Home & Kitchen',
Url: 'http://www.amazon.in/Home-City-Gloria-Seater-Sectional/dp/B018QWEL1W/r
ef=lp_5689464031_1_6?s=kitchen&ie=UTF8&qid=1462946106&sr=1-6',
userId: 56fe44836ce2226431f5388f,
purchased: 'yes',
contributed: '' },
{ _id: 5732c8e6599bfcc031013925,
Product_Name: 'Symphony Diet 12T 12-Litre Air Cooler (White)',
Brand: 'Symphony',
Color: '',
Image: 'http://ecx.images-amazon.com/images/I/31YqUvEFvLL._SY300_QL70_.jpg',

Price: '6,100.00',
Rating: '3.5 out of 5 stars',
Description: 'Standard Water Storage With Powerful Cooling The Symphony Diet
12t cooler is a compact air cooler for your home. This device is efficient     and
perfect for day-to-day use. ',
Category: 'Home & Kitchen',
Url: 'http://www.amazon.in/Symphony-Diet-12-Litre-Cooler-White/dp/B00IYD419Q
/ref=sr_1_1?s=kitchen&ie=UTF8&qid=1462945947&sr=1-1&keywords=cooler',
userId: 56fe44836ce2226431f5388f,
purchased: 'yes',
contributed: '' } ]`

现在我想将它们像var resultOfTwo ['product'] ['purchase']这样存储起来。

1 个答案:

答案 0 :(得分:0)

试试这个(这是一个想法)

function convert(obj) {
    var multiArray = [];
    for(var key in obj) { multiArray.push([ key, obj[key] ]); }
    return multiArray;
}

var myMultiArray = convert(customObject);