为什么我的日期上的$ sort运算符在查询中不一致? [英] Why isn't my $sort Operator on dates function consistent in my query?

查看:102
本文介绍了为什么我的日期上的$ sort运算符在查询中不一致?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个查询,以便根据...{sort: {paymentDate: -1 }}降序对文档进行排序.第一次运行查询时,查询部分{sort: {paymentDate: -1 }}似乎被忽略了! 但是,当我在浏览器中刷新页面时,将应用查询部分 {sort: {paymentDate: -1 }},并且查询将以正确的排序顺序显示.

I am trying to write a query to sort out documents based on descending dates ...{sort: {paymentDate: -1 }} order. The first time the query runs, the query section {sort: {paymentDate: -1 }} seems get ignored! However when I refresh the page in the browser, the query section {sort: {paymentDate: -1 }} is applied, and the query displays in the correct sort order.

我需要知道如何解决此问题!

I need to know how to correct this issue!

在浏览器控制台中运行recipientsDetails.find().fetch();查询后,在文档内容下方找到:

Find below the contents of my document after I run the recipientsDetails.find().fetch(); query in the browser console:

0:
payersUserId: "hbieZBFNE53GpE8LP"
paymentDate: "2019-02-11 02:37:05"
payersNumber: "+25478887633"
paymentStatus: "Failed"
recipientNumber: "+25478887633"
_id: "eFShDRzp9JM9ejG5S"

1:
payersUserId: "hbieZBFNE53GpE8LP"
paymentDate: "2019-02-08 16:02:25"
payersNumber: "+2547078887633"
paymentStatus: "Failed"
recipientNumber: "+25478887633"
_id: "SnpNwsx49mZfPNSg7"

2:
payersUserId: "hbieZBFNE53GpE8LP"
paymentDate: "2019-02-08 15:00:02"
payersNumber: "+254707888633"
paymentStatus: "Failed"
recipientNumber: "+25478087703"
_id: "ZHWSiRBYk2xoZvDzb"

以上结果也是所需的排序顺序.

The above results is also the desired sorted order.

也许下面的 helper 代码可能会遮挡一些光线.

Perhaps the below helper code might shade some light.

../client/main.js

Template.paymentB2C.helpers({

'enableButton': function () {

    var enableButtonStatusArray = [];   
    var userIdCode = Meteor.userId(); 
    var phoneNumber = Meteor.users.findOne({_id: userIdCode }, { fields: { "profile.telephoneNumber": 1 } } ); 
    var usersPhoneNumber = phoneNumber.profile.telephoneNumber; 

    var selectedRecipientDetails = recipientsDetails.find( { $or: [ { payersNumber: usersPhoneNumber }, { recipientNumber: usersPhoneNumber } ] }, 

                                            { fields: { 
                                                        "payersUserId": 1,
                                                        "paymentDate": 1,
                                                        "paymentStatus": 1,
                                                        "_id": 1

                                            } }).fetch();


         selectedRecipientDetails.forEach((user) => {
                    payersUserId = user.payersUserId;
                    paymentDate = user.paymentDate;
                    paymentStatus = user.paymentStatus;
                    _id = user._id;

                if(paymentStatus === "Failed"){

                        enableButtonStatusArray.push({ 
                            paymentStatus: paymentStatus,
                            paymentDate: paymentDate,
                            _id: _id
                            });

                    }

            else if(paymentStatus === "Passed"){

                        enableButtonStatusArray.push({ 
                            paymentStatus: paymentStatus,
                            paymentDate: paymentDate,
                            _id: _id});

                   }

            Session.set('enableButtonStatusArray2', enableButtonStatusArray );

        });

    var enableButtonStatusArrayForPrint = Session.get('enableButtonStatusArray2');

return enableButtonStatusArrayForPrint;

}


});

请注意,此处的查询缺少...{sort: {paymentDate: -1 }}函数.

Note that the query here lacks a ...{sort: {paymentDate: -1 }} function.

在我的Router代码下面找到:

../client/main.js

../client/main.js

Router.route('/paymentB2C', {
name: 'paymentB2C',  
template: 'paymentB2C',

    waitOn: function(){

       return Meteor.subscribe('pendingPayments')

    }

});

这导致我的Meteor.subscribe('pendingPayments')发布功能:

../server/main.js

Meteor.publish('pendingPayments', function pendingPayments(){

   return recipientsDetails.find({}, {sort: {paymentDate: -1 }});

});

请注意,这是我具有sort功能的地方.

Note that here is where I have the sort function.

有人可以解释为什么当第一次运行代码时,会忽略 sort 并随机对文档进行排序,但是在浏览器中刷新后按设计(正确)进行了排序吗?

Can someone explain why when codes first runs, the sort is ignored and the the document is randomly sorted, however is sorted out as designed (correctly) after hitting refresh in the browser?

期待您的帮助.

推荐答案

理想地,您应在订阅后对客户端查询中的数据进行排序,而不是在publish方法中对其进行排序.

Ideally, you should sort the data on the client-side query after you subscribe, instead of sorting it in the publish method.

原因是,如果客户端预订了多个发布功能,这些功能将发布来自相同集合的数据,那么您在客户端的find查询将可以访问来自发布和排序的数据没效果.此外,发布是一种向订户授予数据访问权限的方法,如果客户端上的mini-mongo已经具有数据,则除非新数据到达,否则它不会同步数据.

The reason is that if the client subscribes to more than one publish function which will publish data from the same collections, your find query in the client-side will have access to the data from both publish as well as sort won't be effective. Moreover, publish is something which will grant data access to the subscriber and if the mini-mongo on the client side already has the data, it won't sync the data unless new data arrives.

因此,您也应该始终在客户端的find查询中进行排序和过滤.

Hence, you should always do sort and filter in your find queries on the client side as well.

此外,我注意到paymentDate字段的格式不是日期".理想情况下,它应为Date格式,并且外观应类似于ISODate("2019-02-11T02:37:05.000Z")而不是String格式"2019-02-11 02:37:05".因此,如果客户端的排序也不起作用,请尝试将paymentDate在数据库中另存为Date而不是String.

Also, I notice that the format of the paymentDate field is not a 'Date'. It should ideally be of the Date format and should look something like ISODate("2019-02-11T02:37:05.000Z") instead of String format "2019-02-11 02:37:05". So if the sorting on the client side is also not working, try saving the paymentDate in the database as Date instead as a String.

这篇关于为什么我的日期上的$ sort运算符在查询中不一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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