gpt4 book ai didi

javascript - 迭代对象数组,并将每个对象保存到 MongoDB

转载 作者:太空宇宙 更新时间:2023-11-03 22:27:33 25 4
gpt4 key购买 nike

我想在从 Facebook 端点接收到数组中的每个对象后将其保存/插入到我的 MongoDB 中。

如何迭代返回的对象数组,然后将其保存到我的 mongoDB 中? :)

来 self 的路线的代码,我在其中使用 superagent 从 Facebook API 接收数据

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": {}
}]

我的模型/架构

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;

最佳答案

您可以迭代数组,然后批量执行所有对象。

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();

关于javascript - 迭代对象数组,并将每个对象保存到 MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43619173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com