按时间戳以升序对 Firestore 数据进行排序

我正在将应用程序发布的想法存储在Firestore中。数据像Ideas/{documentID}/IdeaObject一样存储在Firestore中。问题是当我检索数据时,它在发布时未排序。检索到的想法是根据他们的文档ID的ID,该ID由Firestore自动创建。我已经在我的模型类中使用了ServerTimestamp,当我检索它时,我将该方法与我的Firestore引用一起使用,但仍然没有任何内容。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);
                        }
                    }
                }); 

我想实现的是检索所有想法,并按TimeStamp值对它们进行排序。


答案 1

在查询数据库时,不能使用字符串 () 而不是日期 (),并且期望其行为与日期相同。因此,要解决此问题,请更改以下代码行:timeStampdate

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

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

为了使其正常工作,此类查询需要一个索引。要创建一个,请检查我从以下帖子中的答案:


答案 2

在我的情况下,香草版本将是,

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

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

“想法”是您收藏的名称

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

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