简体   繁体   English

Mongo的查找操作未按预期工作

[英]Mongo's find operation not working as expected

Below is the code that works well: 下面是运行良好的代码:

for(i=0;i<distinct_branch.length;i++){
                    myobj = {}
                    myobj['branch'] = distinct_branch[i]
                    myobj['total_re'] = 0
                    myobj['present'] = 0
                    myobj['absent'] = 0
                    myobj['weekly_off'] = 0
                    myobj['leave'] = 0
                    myobj['total_marked'] = 0
                    myobj['pending_attend'] = 0
                    myobj['dat_compliance'] = 0
                    myobj['pending_dat_compliance'] = 0
                    for(j=0;j<results.length;j++){
                        if(results[j].counter){
                            if(results[j].counter.state){
                                if(distinct_branch[i]==results[j].counter.state){
                                    myobj['total_re'] = myobj['total_re'] + 1
                                    if(results[j].attendance == 'P') {
                                        myobj['present'] = myobj['present'] + 1
                                        myobj['total_marked'] = myobj['total_marked'] + 1
                                    }
                                    else{
                                        if(results[j].attendance == 'A') {
                                            myobj['absent'] = myobj['absent'] + 1
                                            myobj['total_marked'] = myobj['total_marked'] + 1
                                        }
                                        else{
                                            if(results[j].attendance == 'O') {
                                                myobj['weekly_off'] = myobj['weekly_off'] + 1
                                                myobj['total_marked'] = myobj['total_marked'] + 1
                                            }
                                            else{
                                                if(results[j].attendance == 'L') {
                                                    myobj['leave'] = myobj['leave'] + 1
                                                    myobj['total_marked'] = myobj['total_marked'] + 1
                                                }
                                                else{
                                                    if(results[j].attendance == 'Pending') {
                                                        myobj['pending_attend'] = myobj['pending_attend'] + 1
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    myobj['dat_compliance'] = ( myobj['total_marked'] / myobj['total_re'])*100
                                    myobj['pending_dat_compliance'] = 100 - myobj['dat_compliance']
                                }
                            }
                        }
                    }
                    returndata.push(myobj)
                }

Now I want that "returndata should have one more property which is the result of a find query" Below is the query: 现在,我希望“ returndata应该再有一个属性,这是查找查询的结果”,下面是该查询:

imports.db.mdb.collection('sales_details').find({'user.empcode':'481625'},function(err,sale_data){
myobj['test']=sale_data;
});
returndata.push(myobj);

//But I have tried each and everything and I am unable to perform this.Node is performing the operation of pushing first and then the query is executed(find). //但是我已经尝试了所有方法,但我无法执行此操作.Node先执行推入操作,然后执行查询(查找)。 I even pushed myobj in the callback of find by comparing if I is equal to distinct_branch.length but that too is not working. 我什至通过比较是否等于distinct_branch.length来将myobj推入find的回调中,但这也无法正常工作。 Thanks in advance :) 提前致谢 :)

The find method work exactly as expected. 查找方法完全按预期工作。 ;-) ;-)

nodejs (googles V8 VM) ist designed to be reactive. nodejs(googles V8 VM)旨在进行反应。 This is due to the fact that there's only one thread running in a nodejs process. 这是由于在nodejs进程中只有一个线程在运行。 As a consequence long running IO-operations perform in its own process parallel to the programm that triggers the IO-access. 结果,长时间运行的IO操作会在自己的进程中执行与触发IO访问的程序并行的过程。

That means if you perform a db.collection.find({}, callback); 这意味着如果您执行db.collection.find({}, callback); the vm requests the mongodb process to search for the data and than returns the control back to the calling programm/function which executes the next statement then the next one and so on. vm请求mongodb进程搜索数据,然后将控件返回给调用程序/函数,该程序将执行下一条语句,然后执行下一条语句,依此类推。 Because a DB search lasts many longer than a value assignment to a variable, the added value is undefined. 因为数据库搜索的持续时间比对变量的值分配要长得多,所以添加的值是不确定的。

As soon as the DB returns the search result, it executes the spezified callback and passing an error variable and a result variable to it. 数据库返回搜索结果后,它将立即执行特定的callback并将错误变量和结果变量传递给该callback You can use the callback to synchronize the find method with the caller. 您可以使用回调将find方法与调用方同步。 ie execute the send method inside of the callback, when it is sure that the data is available. 当确定数据可用时,即在回调内部执行send方法。

An example: Let me assume that you use the find method to answer a (let's say) express route request. 一个示例:让我假设您使用find方法来回答(比如说)快速路由请求。

...
router.get(url, function(req,res,next)
{
   db.find({...}, function(err, sale_data)
   {
       if ( err )
       {
           next(err);
       }
       myobj['test']=sale_data;    
       returndata.push(myobj);
       resp.json(returndata);
   });
});

Then the request is send as soon as the data is available. 然后,一旦数据可用,就发送请求。

Synchronizing is easy for your example, but as soon as you use the returned data to start a new asynchronous request to the DB and use the return data to start ... Then it is best practise to us a synchronization framework like async or promises like handled by Kris Kowalskis Q . 对于您的示例而言,同步很容易,但是一旦您使用返回的数据向数据库发起新的异步请求并使用返回的数据开始...那么,对于我们来说,最佳实践是像async或promises这样的同步框架由Kris Kowalskis Q处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM