Node.js,MongoDB-插入/更新多个文档并发送单个响应 [英] Node.js, MongoDB - Inserting/updating multiple documents and sending a single response

查看:64
本文介绍了Node.js,MongoDB-插入/更新多个文档并发送单个响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个同步服务器(例如:SVN之类),该服务器在一个请求(JS对象的JSON字符串化数组)中从客户端接受一个或多个文档(JSON字符串),将其插入/更新到mongodb中,发送一个响应-这是一个JSON字符串,其中包含每个文档的插入/更新状态(以及mongo中的_id等其他信息).

I'm trying to develop a synchronization server (think: SVN like) that accepts one or more documents (JSON string) from the client in one request (JSON stringified array of JS objects), inserts/updates them into mongodb and sends one response - which is a JSON string containing the insert/update status (and some more info like _id in mongo) of each document.

如果这是一个文档,则可以完成一次插入/更新,而在其回调中,我可以发送响应.

If it was one document, i could have done one insert/update and in its callback, i could have sent the response.

collection.insert(_data, function(error, result) {
    if(error) res.send('error');
    else res.send(JSON.stringify({result: result}));
});

但是当我有多个文档时,该怎么做.我可以在上一个回调中插入/更新一个文档.但是我担心,如果这样做的话,最终将导致可怕的代码阶梯(我可以在一个函数中执行并递归,是的).

But how to do this when i have more than one document. I could do insert/update one document in the previous one's callback. But i fear i'll end up with a dreadful ladder of code if i do that (i could do it in one function and recurse, yes).

任何帮助将不胜感激.顺便说一句,我正在使用此驱动程序: http://mongodb.github.io/node-mongodb -native/

Any help will be greatly appreciated. BTW, i'm using this driver: http://mongodb.github.io/node-mongodb-native/

注意:我不是在查看批处理插入或更新,因为正在处理的每个文档都需要单独处理.有些可能需要插入,一些需要更新,然后有版本号&.同步状态检查等.

Note: I'm not looking at batch inserts or updates, as each document that is being processed needs individual processing. Some may need to be inserted, some updated, and then there is version number & sync-status checking etc.

推荐答案

您可能需要尝试异步模块.它具有一些非常有用的方法来处理集合中的每个项目,并为所有处理完成后提供功能.

You might want to try the async module for this. It has some very helpful methods for processing each item in a collection, and offers functionality for when all processing has finished.

我特别推荐您使用队列函数,该函数可将任务添加到队列中,然后在处理完所有项目后,执行一些操作.

I refer you in particular to the queue function, which lets you add tasks to a queue, then once all items have been processed, do something.

例如,您可能会做类似的事情:

For instance, you might do something like:

var q = async.queue(function(task, callback) {
  // task.doc would contain your individual doc to process

  // your insert / update logic goes here...

  // Callback signifies you're done with this task
  callback();

}, 2) // <--- this number specifies the number of tasks to run in parallel

q.drain = function() {
  // this is the queue's callback, called when the queue is empty,
  // i.e. when all your documents have been processed.

  res.send(statusCode, message);
} 

然后,如果我们假设您将文档列表包含在名为 docs 的变量中,则处理所有文档所需要做的就是将它们压入队列.

Then, if we assume you have your list of documents in a variable named docs, all you need to do to process them all is push them onto the queue.

for (var doc in docs) {
  q.push({ doc: docs[doc] }, function(err) {
    if (err) console.log(err);
  })
}

提示:您需要将包含文档的对象 推入队列.如果您尝试传递未包装的对象,则会出现一个奇怪的错误.

Protip: you need to push an object containing the document onto the queue. There's a weird error if you try to pass in an unwrapped object.

现在,如果您想要在Mongo中处理的每个文档的特定状态,则完全可以这样.只要实例化队列外部的数据结构,就可以在处理每个项目时向其添加statusCodes等,然后通过队列的 drain 函数将该结构发送给客户端.不应太麻烦.

Now, if you wanted the specific statuses for each document you process in Mongo, this is totally possible like this. As long as you instantiated a data structure outside the queue, you could add statusCodes (etc.) to it as each item is processed, and send the structure to the client in the queue's drain function. Shouldn't be too much hassle.

这篇关于Node.js,MongoDB-插入/更新多个文档并发送单个响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆