2017-04-25 175 views
0

我想在從Facebook端點接收數據後,將每個對象從數組保存/插入到我的MongoDB中。迭代對象數組,並將每個對象保存到MongoDB

我將如何遍歷返回的對象數組,然後將其保存到我的mongoDB? :)從我的路線,在那裏我從Facebook的API接收數據用的SuperAgent

代碼

var express = require('express'); 
var router = express.Router(); 
var request = require('superagent') 
var utils = require('../utils') 

router.get('/facebook', function(req, res, next) { 

    var url = "https://graph.facebook.com/v2.9/xxxxxxxx" 

    request 
    .get(url) 
    .set('Accept', 'application/json') 
    .end(function(err, result){ 
     var parse = JSON.parse(result.text) 
     res.json({ 
     confirmation: 'success', 
     response: parse 
     }) 
    }); 

}); 

module.exports = router; 

每個對象看起來像這樣:

"confirmation": "success", 
"response": { 
"data": [ 
{ 
"message": "Søger et godt hjulsæt til Max 1000\nkr.1,000 - Løsning, Vejle, Denmark\n\nVed at ser er en masse fine hjulsæt er er i overskud der ude er der nogen der har et rigtig fint hjulsæt i vil sælge mig ?", 
"from": {}, 
"permalink_url": "https://www.facebook.com/groups/306677009409252/permalink/1333896396687303/?sale_post_id=1333896396687303", 
"type": "photo", 
"created_time": "2017-04-25T14:26:39+0000", 
"picture": "https://scontent.xx.fbcdn.net/v/t1.0-0/q81/s130x130/18157109_10155237818408609_6730263000530603997_n.jpg?oh=e4198809641ce4549592065f9ce1d522&oe=5993A162", 
"id": "306677009409252_1333896396687303", 
"attachments": {} 
}, 
{ 
"message": "Bontrager cykelhjelm\nkr.250 - Lemvig\n\nNy cykelhjelm bontrager starvos str S\n51-57 cm ny og i original emballage. Ny pris 649 kr", 
"from": { 
"name": "Tanja Olsen", 
"id": "10155372412188754" 
}, 
"name": "Photos from Tanja Olsen's post", 
"permalink_url": "https://www.facebook.com/groups/306677009409252/permalink/1333870013356608/?sale_post_id=1333870013356608", 
"type": "photo", 
"created_time": "2017-04-25T13:49:26+0000", 
"picture": "https://scontent.xx.fbcdn.net/v/t1.0-0/s130x130/18118873_10155371971413754_3345993522211660552_n.jpg?oh=de0d10dc9e0589789e023819402ef399&oe=598FAE09", 
"id": "306677009409252_1333870013356608", 
"attachments": {} 
}] 

我的模型/ Schema

var mongoose = require('mongoose') 

var FeedSchema = new mongoose.Schema({ 
    type: {type: String, default: ''}, 
    title: {type: String, default: ''}, 
    image: {type: String, default: ''}, 
    description: {type: String, default: ''}, 
    timestamp: {type: Date, default: Date.now} 
}) 

module.exports = mongoose.model('FeedSchema', FeedSchema) 

我曾嘗試,但沒有工作尚未

var express = require('express'); 
var router = express.Router(); 
var request = require('superagent') 
var utils = require('../utils') 
var Feed = require('../models/Feed') 

router.get('/facebook', function(req, res, next) { 

    var url = "https://graph.facebook.com/v2.9/XXXXXXX" 
    var bulk = Feed.initializeUnorderedBulkOp(); 

    request 
    .get(url) 
    .set('Accept', 'application/json') 
    .end(function(err, result){ 
     var parse = JSON.parse(result.text) 

     var object = parse.data 

     array.forEach((object) => { 
     bulk.insert({ 
      type: object.type, 
      title: object.title, 
      image: object.image, 
      description: object.description 
     }) 
     }) 
     bulk.execute(); 
    }); 


}); 

module.exports = router; 
+0

爲什麼你迭代對象,你是否想在將對象插入到你的db之前改變對象。你還有什麼模型模式? 有沒有理由不使用$ pushAll''Bulk.insert()'? –

+0

我想保存數組中的每個對象,然後我必須遍歷每個對象的權利或是一個錯誤的方法?我用模型 – DbTheChain

+0

更新了帖子,好像你想要映射數組並返回具有所需屬性的對象數組。然後你可以做一個bulk.insert來一次插入所有的對象。請參閱https://docs.mongodb.com/manual/reference/method/Bulk.insert/ –

回答

0

可以迭代這個數組,然後bulk.execute所有對象。

var bulk = db.items.initializeUnorderedBulkOp(); 
array.forEach((object) => { 
    bulk.insert({ 
    type: object.type, 
    title: object.title, 
    image: object.image, 
    description: object.description, 
    timestamp: object.timestamp 
    }) 
}) 
bulk.execute(); 
+0

非常感謝,並且在我的路線中,我應該在.end之後放置它嗎?同樣在var bulk = db.items.initializeUnorderedBulkOp()中,我應該用我的模型或數據庫來替換數據庫? – DbTheChain

+0

也是。在數據返回後直接進行。 –

+0

酷感謝,我試過你的建議,但我似乎無法得到它的工作,你能檢查我在帖子中添加的代碼嗎? :) – DbTheChain