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>
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", //
-OKR
nodes seem correct to me, as-OKU6l
comes before-OKY6m
alphabetically. – Frank van Puffelen Commented Mar 4 at 14:16