gpt4 book ai didi

mongodb - 如何过滤在其属性之一中包含某些字符串的 mongo 文档?

转载 作者:可可西里 更新时间:2023-11-01 09:33:07 26 4
gpt4 key购买 nike

我有一个包含一些基本用户信息的用户模型。我只是为我的应用程序添加搜索功能,我想找到在其 displayName 属性中包含搜索词的用户?

用户模型

[
{
"_id" : ObjectId("5bbda46a433ced65ac7c4699"),
"email" : "henoktes72@gmail.com",
"displayName" : "Henok Tesfaye",
"userId" : "5bbda46a433ced65ac7c4698",
"reputationNumber" : 0,
"questions" : [
"5bbf135d7883513e3839a34c"
],
"answers" : []
},
{
"_id" : ObjectId("5bbdb84a7da23035740bf056"),
"email" : "mezi@gmail.com",
"displayName" : "Mezi Villager",
"userId" : "5bbdb84a7da23035740bf055",
"reputationNumber" : 5,
"questions" : [
"5bbf1f137883513e3839a34f"
],
"answers" : [
"5bbf6933e564763770d81828"
]
}
]

如果搜索词是 Henok,则输出必须是

[
{
"_id" : ObjectId("5bbda46a433ced65ac7c4699"),
"email" : "henoktes72@gmail.com",
"displayName" : "Henok Tesfaye",
"userId" : "5bbda46a433ced65ac7c4698",
"reputationNumber" : 0,
"questions" : [
"5bbf135d7883513e3839a34c"
],
"answers" : []
}
]

.find({ displayName: "Henok"}) 返回空记录。

最佳答案

您可以在 where cond 的环回中使用 filter

Two way to use regex in loopback.

Model.find({
where: {'displayName': /video/i }, //i for case-insensitive.
}, function(err, resilus) {
console.log(resilus);
});

OR

var search = new RegExp(search, 'i'); //i for case-insensitive.

Model.find({
where: {'displayName': search },
}, function(err, resilus) {
console.log(resilus);
});

关于mongodb - 如何过滤在其属性之一中包含某些字符串的 mongo 文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52774834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com