【问题标题】:Meteor / python / mongoDB - How do I update the mongoDB from a background running process?Meteor / python / mongoDB - 如何从后台运行进程更新 mongoDB?
【发布时间】:2020-03-15 17:28:38
【问题描述】:

我已经开始创建一个流星应用程序,我想将文档插入我使用后台任务定义的一个 mongoDB 集合中。

例如,假设我每天都在阅读 RSS 提要并在网站上抓取新闻标题。

要求

  1. 阅读 rss 提要并抓取必要的网站。
  2. 将新标题放入流星应用使用的 mongoDB 集合中。

我所知道的

我知道怎么做 1. 使用python,requests 和其他库。 我知道如何通过流星中的javascript将信息添加到集合中,但按照todo示例应用程序教程通过后台任务。

可能的解决方案

  1. 在 python 中获取数据,然后通过 python 将该数据添加到 mongoDB 集合中。然后,这将在某种后台进程的服务器上运行。
  2. 使用某些来自流星的库,它允许我运行后台任务和网络抓取,然后我可以使用它来更新集合。

最小的例子来自this step of the todo app

我要自动化的步骤:

meteor mongo
db.tasks.insert( { text : 'New title' , createdAt: new Date() } );

其中New title 是一些报废/RSS 提要的结果。

实现这一目标的最佳方法是什么?我对python比较流利,但对JS和meteor完全陌生。

其他cmets

此外,我没有在下面的示例中包含此内容,但我还想使用 accounts-uiaccounts-password 包,并将删除 insecure 包。不确定这如何影响后台进程的实现。

小例子

(对不起,如果这不是非常小,但不知道如何使它更小)

  • 客户端
    • main.html
    • main.jsx
  • 服务器
    • main.js
  • 进口
    • API
      • tasks.js
    • 用户界面
      • App.jsx
      • Task.jsx

客户端/main.html

<head>
  <title>Todo List</title>
</head>
<body>
  <div id='render-target'></div>
</body>

client/main.jsx

import React from 'react';                                                                  
import { Meteor } from 'meteor/meteor';
import { render } from 'react-dom';

import App from '../imports/ui/App.jsx';

Meteor.startup( () => {
    render( <App/> , document.getElementById( 'render-target' ) );
});

服务器/main.js

import '../imports/api/tasks.js'

imports/api/tasks.js

import { Mongo } from 'meteor/mongo';                                                       

export const Tasks = new Mongo.Collection( 'tasks' );

imports/ui/App.jsx

import React , { Component , PropTypes } from 'react';                                      
import { createContainer } from 'meteor/react-meteor-data';

import { Tasks } from '../api/tasks.js';

import Task from './Task.jsx';

class App extends Component {

    renderTasks() {
        return this.props.tasks.map( ( task ) => (
            <Task key={task._id} task={task} />
        ) );
    }   

    render() {
        return (
            <div className='container'>
                <header>
                    <h1>Todo List</h1>
                </header>

                <ul>
                    { this. renderTasks() }
                </ul>
            </div>
        );
    }   
}

App.propTypes = { 
    tasks : PropTypes.array.isRequired,
};

export default createContainer( () => {
    return {
        tasks : Tasks.find( {} ).fetch(),
    };  
} , App );

imports/ui/Task.jsx

import React, { Component , PropTypes } from 'react';                                       

// Task component - represents a single todo item
export default class Task extends Component {
    render() {
        return (
            <li>{this.props.task.text}</li>
        );
    }   
}

Task.propTypes = { 
    // This component gets the task to display through a React prop.
    // We can use propTypes to indicate it is required
    task: PropTypes.object.isRequired,
};

【问题讨论】:

    标签: python mongodb meteor


    【解决方案1】:

    使用您最了解的语言和工具进行抓取和插入,因为您提到了 python,我认为这将是您的选择。 Pymongo 使得将 python dict 写入 mongo 变得非常容易。

    您可以运行自己的 mongo 版本或访问默认版本。要获取默认的连接字符串,请 cd 进入流星目录并输入:

    $ meteor mongo -U
    

    它可能类似于 mongodb://127.0.0.1:3001/meteor。所以,要从 python 连接到它:

    from pymongo import MongoClient
    connection = MongoClient("mongodb://127.0.0.1:3001/meteor")                           
    db = connection.meteor.tasks
    

    然后进行插入:

    db.insert({ 'text' : title , 'createdAt': datetime.datetime.utcnow()} )
    

    如果您使用自己的数据库,则反应式流星组件将每十秒更新一次(它会轮询)。如果您使用默认值,它将是即时的。

    【讨论】:

    • 你是上帝派来的!好的听起来像从 python 做是要走的路。我的最后一个问题是,从 python 执行此操作我是否必须担心身份验证等?即就像在 mongo 中你向客户端“发布”一个方法一样,我是否必须向我的 python 脚本发布一些东西?
    • 我想通了。我只是能够独立于身份验证等在数据库中插入项目。回想起来,这似乎很明显,因为我猜数据库本身没有对任何东西进行身份验证
    • 对不起,我没有回答!我才注意到你的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    相关资源
    最近更新 更多