代码之家  ›  专栏  ›  技术社区  ›  Ebad Ali

按时间戳按升序排列Firestore数据

  •  31
  • Ebad Ali  · 技术社区  · 6 年前

    我正在Firestore中存储应用程序发布的想法。数据存储在Firestore中,如下所示 想法/{documentID}/IdeaObject 。问题是当我检索数据时,它在发布时未按w.r.t排序。根据Firestore自动创建的文档id的id,检索到的创意都在其中。我用过 服务器时间戳 在我的模型类中以及检索它时,我使用 orderBy 方法,但仍然没有任何内容。

    主意JAVA

    public class Idea {
        @ServerTimestamp
        private Date date;
        private String title, idea, timeCommitment, ideaStage, postedBy, website, videoPitch;
        private int views, favorites;
        private ArrayList<String> lookingFor = new ArrayList<>();
        private ArrayList<String> tags = new ArrayList<>();
        private String userID;
        private String timeStamp;
    
        public Idea() {
        }
    
        public Idea(String title, String idea, String timeCommitment, String ideaStage, String postedBy, String website, String videoPitch, int views, int favorites, ArrayList<String> lookingFor, ArrayList<String> tags, String userID, String timeStamp) {
            this.title = title;
            this.idea = idea;
            this.timeCommitment = timeCommitment;
            this.ideaStage = ideaStage;
            this.postedBy = postedBy;
            this.website = website;
            this.videoPitch = videoPitch;
            this.views = views;
            this.favorites = favorites;
            this.lookingFor = lookingFor;
            this.tags = tags;
            this.userID = userID;
            this.timeStamp = timeStamp;
        }
    

    想法发布方法

      private void postIdea() {
    
            final String ideaID = UUID.randomUUID().toString();
            final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
    
    
            Idea ideas = new Idea(title, idea, timeCommitment, ideaStage, AppValues.fullName, website, videoPitch, 0, 0, lookingFor, tags, AppValues.userId, "" + timestamp.getTime());
    
            firestoreDb.collection("ideas")
                    .document(ideaID)
                    .set(ideas)
                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            postIdeaUser(ideaID);
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            hideLoadingDialog();
                            showToast(e.getMessage());
                        }
                    });
        }
    

    按时间检索所有想法

       firestoreDb.collection("ideas")
                    .orderBy("timeStamp", Query.Direction.ASCENDING)
                    .get()
                    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            if (task.isSuccessful()) {
    
                                ideaArrayList = new ArrayList<>();
                                ideaArrayList.clear();
    
                                for (DocumentSnapshot documentSnapshot : task.getResult()) {
                                    Idea idea = documentSnapshot.toObject(Idea.class);
                                    if (!idea.getUserID().equals(AppValues.userId)) {
                                        ideaArrayList.add(idea);
                                    }
                                }
    
                                callAdapter();
    
                            } else {
                                Log.d(TAG, "Error getting documents: ", task.getException());
                                progressBar.setVisibility(View.GONE);
                                swipeRefresh.setEnabled(true);
                                errorText.setVisibility(View.VISIBLE);
                            }
                        }
                    }); 
    

    我想要实现的是检索所有想法,并按时间戳值升序排列它们。

    5 回复  |  直到 6 年前
        1
  •  47
  •   Alex Mamo    3 年前

    不能使用字符串( timeStamp )查询数据库而不是日期时( date )并期望表现得像约会一样。因此,要解决此问题,请更改以下代码行:

    firestoreDb.collection("ideas")
                .orderBy("timeStamp", Query.Direction.ASCENDING)
    

    firestoreDb.collection("ideas")
                .orderBy("date", Query.Direction.ASCENDING)
    

    要使其工作,这种查询需要一个索引。要创建一个,请检查我在以下帖子中的答案:

        2
  •  13
  •   Vince Banzon    5 年前

    就我而言,香草版应该是,

    firestoreDb.collection("ideas")
         .orderBy("timestamp", "asc")
    
    firestoreDb.collection("ideas")
         .orderBy("timestamp", "desc")
    

    “创意”是您收藏的名称

    “时间戳”是用于排序的键或字段或列名。

    “asc”或“desc”是用于订单的选项

        3
  •  1
  •   Jeevan Rupacha    4 年前

    您还必须在数据库中的firebase帐户中添加索引->索引->添加索引

        4
  •  0
  •   Ankit Mishra    3 年前

    只需创建索引,它也将开始使用时间戳。只需确保时间戳是数字类型,而不是字符串。

    创建索引的最佳方法是搜索 FAILED_PRECONDITION: 在Android Studio logcat中,您将看到一个链接,只需跟随该链接并创建索引即可。
    示例: FAILED_PRECONDITION: The query requires an index. You can create it here: ...

    请随时提出任何疑问。

        5
  •  0
  •   Adan Vivero    3 年前

    您可以按照以下步骤在控制台上订购firebase数据。

    步骤1: 单击文档集合中的这三行。

    enter image description here

    第2步: 从那里,您可以得到要过滤的字段。您可以输入哪个字段,在我的例子中是“timestamp”。

    enter image description here

    步骤3: 选择要应用的排序结果,对于我的情况,它是“asc”,然后单击“应用”,它应该在数据库中排序您想要的结果。