admin管理员组

文章数量:1410730

I'm writing in the realtime database using the push() function

The push() function orders the input data in chronological order, but it does not, and it orders the data in numerical order

I want the data to be entered in chronological order The userName is not based on timestamp, but the data is recorded in the order of 0107 after 0104

"0104": {
  "-OKQu5uz3y6aq_exRrwA": {
    "contents": "1",
    "timestamp": "2025-03-03T12:08:34.229Z",
    "userName": "0104"
  }
},
"0107": {
  "-OHJRj4ye8DOxw0UbEo1": {
    "contents": "1",
    "timestamp": "2025-01-23T18:35:48.476Z",
    "userName": "0107"
  }
},
"0107": {
  "-OJRQ1IGgYYgmk-ri7Ye": {
    "contents": "1 ",
    "timestamp": "2025-02-19T04:16:44.235Z",
    "userName": "0107"
  },

<script type="module">  
    import { initializeApp } from ".1.0/firebase-app.js";
    import { getDatabase, ref, set, get, child, push } from ".1.0/firebase-database.js";
    import { getAnalytics } from ".1.0/firebase-analytics.js";
    
    const firebaseConfig = {My Firebase Info};

    // Initialize Firebase
    const app = initializeApp(firebaseConfig);
    const analytics = getAnalytics(app);
    const db = getDatabase(app);

    document.getElementById("submit").addEventListener('click', function (e) {
        e.preventDefault();
        const currentTime = new Date().toISOString(); // Get current time in ISO format

        push(ref(db, 'user/' + document.getElementById("userName").value), {
            userName: document.getElementById("userName").value,
            contents: document.getElementById("contents").value,
            timestamp: currentTime // Add timestamp
        });

        alert("Success");
    });
</script>

I'm writing in the realtime database using the push() function

The push() function orders the input data in chronological order, but it does not, and it orders the data in numerical order

I want the data to be entered in chronological order The userName is not based on timestamp, but the data is recorded in the order of 0107 after 0104

"0104": {
  "-OKQu5uz3y6aq_exRrwA": {
    "contents": "1",
    "timestamp": "2025-03-03T12:08:34.229Z",
    "userName": "0104"
  }
},
"0107": {
  "-OHJRj4ye8DOxw0UbEo1": {
    "contents": "1",
    "timestamp": "2025-01-23T18:35:48.476Z",
    "userName": "0107"
  }
},
"0107": {
  "-OJRQ1IGgYYgmk-ri7Ye": {
    "contents": "1 ",
    "timestamp": "2025-02-19T04:16:44.235Z",
    "userName": "0107"
  },

<script type="module">  
    import { initializeApp } from "https://www.gstatic/firebasejs/11.1.0/firebase-app.js";
    import { getDatabase, ref, set, get, child, push } from "https://www.gstatic/firebasejs/11.1.0/firebase-database.js";
    import { getAnalytics } from "https://www.gstatic/firebasejs/11.1.0/firebase-analytics.js";
    
    const firebaseConfig = {My Firebase Info};

    // Initialize Firebase
    const app = initializeApp(firebaseConfig);
    const analytics = getAnalytics(app);
    const db = getDatabase(app);

    document.getElementById("submit").addEventListener('click', function (e) {
        e.preventDefault();
        const currentTime = new Date().toISOString(); // Get current time in ISO format

        push(ref(db, 'user/' + document.getElementById("userName").value), {
            userName: document.getElementById("userName").value,
            contents: document.getElementById("contents").value,
            timestamp: currentTime // Add timestamp
        });

        alert("Success");
    });
</script>
Share Improve this question edited Mar 4 at 14:17 Frank van Puffelen 601k85 gold badges890 silver badges860 bronze badges Recognized by Google Cloud Collective asked Mar 4 at 3:16 DebugDebug 12 bronze badges 4
  • Which data in the screenshot is not ordered in the way you want it to? --- It might help if you show the JSON as text, rather than a screenshot. You can export the JSON text from the Firebase console. – Frank van Puffelen Commented Mar 4 at 3:22
  • Please edit your question and add the information Frank asked for along with the query that doesn't return the desired results, and please also respond using @. – Alex Mamo Commented Mar 4 at 4:36
  • Thanks for replacing the screenshot with JSON data. Now can you clarify which data in the screenshot/JSON that you shared is not in the correct order? --- The two -OKR nodes seem correct to me, as -OKU6l comes before -OKY6m alphabetically. – Frank van Puffelen Commented Mar 4 at 14:16
  • @FrankvanPuffelen 0104 or 0105 number is userName I want userName to be sorted in chronological order, but if you look at screenshot now, you can see it's sorted in numerical order like 0104 > 0105 > 0107 instead of timestamp – Debug Commented Mar 5 at 0:10
Add a comment  | 

1 Answer 1

Reset to default 0

Data in the Firebase Realtime Database dashboard/console is always sorted lexicographical based on the keys. So your keys on the "0104" level are sorted on that exact value (with "104" appearing before "107", etc).

There is no way to change the order how the Firebase console displays these values.


If the ability to see them in a different order is crucial during your development cycle, consider building a small custom dashboard where you use the ordering operations of the API.

Note though that in that case too, you won't be able to sort the "0104" level of the data on the push keys or timestamp values that you currently have, as each node on that "0104" level must have exactly one value at a fixed path under it to sort it on.

As usual when dealing with NoSQL databases, you can work around this problem by adding some additional data. For example by adding a sortValue under each such node:

"0104": {
  "sortValue": "-OKQu5uz3y6aq_exRrwA", // 

本文标签: javascriptFirebase realtimedatabase sort to timeStack Overflow