在mongoose中更新具有不同值的多组记录的最佳方法是什么?

时间:2016-10-30 19:11:30

标签: node.js mongoose

例如using UnityEngine; using System.Collections; public class swipeCamera: MonoBehaviour { Vector3 hit_position = Vector3.zero; Vector3 current_position = Vector3.zero; Vector3 camera_position = Vector3.zero; float z = 0.0f; // Use this for initialization void Start() { } void Update() { if (Input.GetMouseButtonDown(0)) { hit_position = Input.mousePosition; camera_position = transform.position; } if (Input.GetMouseButton(0)) { current_position = Input.mousePosition; LeftMouseDrag(); } } void LeftMouseDrag() { // From the Unity3D docs: "The z position is in world units from the camera." In my case I'm using the y-axis as height // with my camera facing back down the y-axis. You can ignore this when the camera is orthograhic. current_position.z = hit_position.z = camera_position.y; // Get direction of movement. (Note: Don't normalize, the magnitude of change is going to be Vector3.Distance(current_position-hit_position) // anyways. Vector3 direction = Camera.main.ScreenToWorldPoint(current_position) - Camera.main.ScreenToWorldPoint(hit_position); // Invert direction to that terrain appears to move with the mouse. direction = direction * -1; Vector3 position = camera_position + direction; transform.position = position; } } 如果我想将值为x的记录更新为5,对于a = y记录我需要将count更改为9,那么我应该如何在mongoose中更新它?

1 个答案:

答案 0 :(得分:0)

只能针对一个查询更新多个文档(通过传递{multi:true}选项)。但是对于多个查询,您可以使用多个调用并执行更新。试试async parallel,它将并行运行您的代码。

async.parallel([
        function(callback) {
            Model.update({a: "x"}, {count: 5}, {multi: true}, function(err, result){
                if(err){
                    callback(err)
                }
                else{
                    callback(null, result);
                }
            })
        },
        function(callback) {
            Model.update({a: "y"}, {count: 9}, {multi: true}, function(err, result){
                if(err){
                    callback(err)
                }
                else{
                    callback(null, result);
                }
            })
        }
    ],
    function(err, results) {
        if(err){
            console.log(err)
        }
        else{
            console.log(results)
        }
    });