在节点 js 中将循环结构转换为 JSON [英] Converting circular structure to JSON in node js

查看:48
本文介绍了在节点 js 中将循环结构转换为 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 5 条记录,当我尝试获取这些记录时遇到以下错误,请有人帮助我.谢谢

I have 5 records and when i try to get those i am facing the following error,can someone help me please.Thanks

我的 nodejs,

exports.searchlistbyfirstname = function (req, res) {
  var params = req.params;
  var record= db.collection('profile');
  record.find( { $text: { $search: params.id} }, (err, result) => {
   if (err){ return console.log(err)
    }
      if(result){
            response = {status:'success',data:result};
        } else{
            response = {status:'fail'};
        }
      res.send(response);
  });

};

推荐答案

$text 和 $search 在您想要搜索集合中的多个字段时很有用,就像您想要搜索可以在多个字段中的字符串一样,例如名字,姓氏等,因此在这种情况下,您可以按以下方式在这些键上创建索引:架构级别:

$text and $search is useful when you want to search multiple fields in a collection like you want to search a string that can be in multiple fields like firstname, lastname etc so in that case you can create indexing on those keys as following ways: Schema level:

new Schema({
    firstname:{type: String, index: true},
    firstname:{type: String, index: true}
    .....
 });

使用确保索引:

profile.collection.ensureIndex({firstname:"text",lastname:"text" ....},default_language:"none"},function(err,result){   });

使用 $text 和 $search 您还可以使用其他语言进行搜索,例如:

你有两条记录:

{ "_id" : 7, "subject" : "coffee and cream", "author" : "efg", "views" : 10 }
{ "_id" : 1, "subject" : "coffee", "author" : "xyz", "views" : 50 }

如果您想用西班牙语进行搜索,那么您可以通过以下方式进行:

and you want to search in Spanish then you can do that in following way:

db.articles.find(
   { $text: { $search: "leche", $language: "es" } }
)

查询返回以下文档:

{ "_id" : 5, "subject" : "Café Con Leche", "author" : "abc", "views" : 200 }
{ "_id" : 8, "subject" : "Cafe con Leche", "author" : "xyz", "views" : 10 }

有关更多详细信息,请参阅此链接:https://docs.mongodb.com/manual/reference/operator/query/文字/

For more details refer to this link: https://docs.mongodb.com/manual/reference/operator/query/text/

这篇关于在节点 js 中将循环结构转换为 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆