September 22, 2024 mongodb
Cara Filter Data Berdasarkan Panjang String di Mongodb
Di mongodb, untuk memfilter data berdasarkan panjang string, anda dapat mengunakan operator $strLenCP
.
Operator $strLenCP
akan mengembalikan UTF-8 code points yang dapat digunakan untuk menghitung panjang suatu string.
Contoh data:
db.posts.insertMany([
{ title: "Amazing Adventure" },
{ title: "Great Escape" },
{ title: "Incredible Journey" },
{ title: "Fantastic Voyage" },
{ title: "Wonderful Discovery" },
{ title: "Awesome Experience" },
{ title: "Exciting Quest" },
{ title: "Interesting Findings" },
{ title: "Breathtaking Moments" },
{ title: "Spectacular Events" }
]);
Misal collection posts
ingin dicari yang panjang judulnya lebih dari 15 karakter.
db.posts.find({
$expr: {
$gt: [{ $strLenCP: "$title" }, 18]
}
});
/* Hasilnya
[
{
_id: ObjectId('66efeaf5f374a3ca7a5e73a0'),
title: 'Wonderful Discovery'
},
{
_id: ObjectId('66efeaf5f374a3ca7a5e73a3'),
title: 'Interesting Findings'
},
{
_id: ObjectId('66efeaf5f374a3ca7a5e73a4'),
title: 'Breathtaking Moments'
}
] */
Contoh di aggregasi:
db.posts.aggregate([
{
$match: {
$expr: {
$gt: [{ $strLenCP: "$title" }, 18]
}
}
}
]);
/* Hasilnya
[
{
_id: ObjectId('66efeaf5f374a3ca7a5e73a0'),
title: 'Wonderful Discovery'
},
{
_id: ObjectId('66efeaf5f374a3ca7a5e73a3'),
title: 'Interesting Findings'
},
{
_id: ObjectId('66efeaf5f374a3ca7a5e73a4'),
title: 'Breathtaking Moments'
}
] */
Referensi:
- https://www.mongodb.com/docs/manual/reference/operator/aggregation/strLenCP/
- https://www.mongodb.com/docs/manual/reference/operator/aggregation/gt/
- https://www.mongodb.com/docs/manual/reference/operator/query/expr/