MongoDB 作为实时数据库

MongoDB as Real-time database

我有使用实时数据库 firestore 的经验。目前,我正在使用 MongoDB 开发 Ionic 3 应用程序。在该应用程序上,我们必须使用拉动刷新功能来更新最新内容。但是如果我们有实时数据库那么我们就不需要有这样的功能。由于上述问题,我的客户现在想使用 firestore。但我们面临的关键问题是数据迁移。即 MongoDB 到 firestore。目前,此应用程序正在生产中(即应用程序商店)并拥有超过 500 名用户。因为将应用程序转换为 firestore 将是一项非常艰巨的任务。所以我的问题是,我们不能将实时数据库功能与 MongoDB 一起使用吗?

注:我用Nodejs/Express作为Restfull api.

你的后端是什么?使用 socket.io 怎么样?

由于您已经在使用 MongoDB 和 Express,这里有一个示例:

服务器文件:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/api/add', function(req, res){
    db.collection('quotes').save(req.body, (err, result) => {
        if (err) return console.log(err)

        // send everyone the added data
        io.emit('ADDED_DATA', req.body);
    });
});

http.listen(3000, function(){
    console.log('listening on *:3000');
});

在您的客户端中:

<script src="/socket.io/socket.io.js"></script>

const socket = io('http://localhost:3030'); //ip and port of server

socket.on('ADDED_DATA', (data) => {
    // manipulate data
    // push to current list
    // or whatever you want
});