I am trying to create a cards game but I am stuck at the point of returning cards with MongoDB: player has 30 cards, when clicking on a card the card does an animation with translate and rotate to reveal the value of the card , and since I only need to reveal the value of 3 cards, I need them to end up in the order in which they were chosen, but when I choose them, it returns them to me sorted by the value that the card has; so, for example, if I choose 1, 2, 3; no problem, but if I choose 2, 1, 3; it returns 1, 2, 3.
I’ve tried sort()
and it doesn’t work, because as I said I need it to return them in the chosen order, neither ascending nor descending (anyway it is sorting the cards without sort
). I have tried with Express Handlebars, but apparently it creates an array, so when I put for example cards.[0].number
; in the case of 2,1,3 it still returns 1 and not 2.
This is my code:
router.post('/cards', (req, res) =>{
let zahl1 = req.body.zahl1;
let zahl2 = req.body.zahl2;
let zahl3 = req.body.zahl3;
cards.find({"zahl": {$in:[zahl1, zahl2, zahl3]}}, { _id: 0}, function(err, cards) {
return res.render('cardsGame', {
cards: cards
});
});
});
Since I am having this issue I am working with a simple HTML to find out how to solve this issue:
</form>
<form method="post" action="/cards" id="bilden">
<input type="text" id="hallo1" class="w-25">
<input type="text" id="hallo2" class="w-25">
<input type="text" id="hallo3" class="w-25">
<a id="funktioniert" onclick="hinrichten()"><input type="submit" value="cards" class="btn btn-primary"></a>
<input type="text" name="zahl1" id="zahl1" target="zahl1" class="w-25">
<input type="text" name="zahl2" id="zahl2" target="zahl2" class="w-25">
<input type="text" name="zahl3" id="zahl3" target="zahl3" class="w-25">
</form>
<script>
let newTextInput1 = document.getElementById('hallo1');
let newTextInput2 = document.getElementById('hallo2');
let newTextInput3 = document.getElementById('hallo3');
let newAncla = document.getElementById('funktioniert');
let inputResult1 = document.getElementById('zahl1');
let inputResult2 = document.getElementById('zahl2');
let inputResult3 = document.getElementById('zahl3');
function hinrichten(){
inputResult1.value = newTextInput1.value;
inputResult2.value = newTextInput2.value;
inputResult3.value = newTextInput3.value;
}
</script>
Can someone help me find a way to do this? Thanks!
2
Answers
Here is how I solve it:
When I got here, I had another problem if my numbers were 1,2,3 everything was ok, but if I had 5,10,25, the code was bringing 10,25,5 since it was just reading the first number, so to solve this in numbers from 1 to 9 I wrote 10 before (since mongoDB Compass didn't allowed me to add just a 0) an to all the other numbers I add just a 1 before, so instead of having numbers from 1 to 30 I have numbers from 101 to 130.
A single MongoDB node returns documents in the order they are encountered. If an index is used to optimize the query, the documents will be encountered in the sorted order of the index.
If you need documents in the order they were inserted into the database, you could hint the $natural index, but that would mean every such query would be a collection scan.
To get them in the order they appear in the request, you will need to sort them on the client side.
Perhaps something like: