Hey So I Want to Make it List only the 10 Newest Objects in a Mongoose Array, Currently it Lists it all
My Current Code:
ProofModel.findOne({ id: guildId }, async (err, data) => {
if(err) throw err;
if(data.Proofs.length) {
embed.setDescription(
data.Proofs.map(
(w, i) =>
`\`${i + 1}\` | User : ${message.guild.members.cache.get(w.user).user.tag} | Proof : [Click Here To Veiw](${w.proof})`
)
)
message.channel.send(embed);
}
});
Schema:
I think you are looking for the $slice operator. Docs are here .
The code you are looking for is something like this:
ProofModel.findOne({ id: guildId } , { Proofs: { $slice : -10 } } , async (err, data) => {
if(err) throw err;
if(data.Proofs.length) {
embed.setDescription(
data.Proofs.map(
(w, i) =>
`\`${i + 1}\` | User : ${message.guild.members.cache.get(w.user).user.tag} | Proof : [Click Here To Veiw](${w.proof})`
)
)
message.channel.send(embed);
}
});
You can use Array#slice
to slice off a select portion of an array. To assist with your problem, you can use negative numbers as arguments so you can cut out elements from the back. Here's a demo:
// sample arr that scaled from 1-100 // [1, 2, 3, ..., 98, 99, 100] const arr = Array.from({ length: 100 }, (v, i) => i + 1); // slice off the last ten elements // for use separately console.log(arr.slice(-10));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.