按时间顺序按TimeStamp订购Firestore数据

时间:2018-05-08 01:48:54

标签: java android firebase timestamp google-cloud-firestore

我正在存储应用程序在Firestore中发布的构思。数据存储在Firestore中,如 Ideas / {documentID} / IdeaObject 。问题是,当我检索数据时,它没有按照发布的时间排序。检索到的想法是根据其文档ID的ID来实现的,该ID由Firestore自动创建。我在我的模型类中使用了 ServerTimestamp ,当我检索它时,我使用orderBy方法和我的Firestore参考,但仍然没有。

Idea.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);
                        }
                    }
                }); 

我想要实现的是检索所有想法并按TimeStamp值按升序排序。

3 个答案:

答案 0 :(得分:7)

在查询数据库而不是日期(timeStamp)时,不能使用字符串(date),并期望表现为日期。所以要解决这个问题,请更改以下代码行:

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

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

答案 1 :(得分:1)

在我的情况下,原始版本为

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

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

“想法”是您收藏的名称

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

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

答案 2 :(得分:0)

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