如何在mongodb的键值对中找到特定的字符串 [英] how to find specific string in key value pair in mongodb

查看:30
本文介绍了如何在mongodb的键值对中找到特定的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mongodb中有这样的数据

<预><代码>[{"name":"西尔维斯特",产品":笔记本电脑,iphone,手机,手机"},{"name":"约翰",产品":自行车,公共汽车,电话,笔记本电脑"},{"name":"富兰克林",产品":自行车,手机"}]

如何在产品密钥中找到笔记本电脑.如果产品密钥看起来像这样

<代码>{"姓名":"XXX",产品":笔记本电脑"}

通过使用这个db.collection.find("product":"laptop");

,我可以很容易地找到那个名字

那么如何找到这个?

另外让我知道这三个网站名称是在使用backbone.js和node.js以及mongodb技术(例如www.trello.com)下运行的.对不起我最糟糕的英语..

解决方案

使用正则表达式mongodb

这对我有用

db.collection.find({"product":/laptop/})

更新答案

如果您想使用变量,请尝试以下操作:

var abc = "laptop";//其他的东西userdetails.find({"product":new RegExp(abc)}).toArray(function(err,result){if (err) console.log ("error:"+err);别的{//如果你想要长度控制台日志(结果.长度);//如果你真的想看到结果for (var i = 0; i < result.length; i++){控制台日志(结果 [i]);}}});

又更新了一次

var abc = "laptop";//其他的东西//注意这是区分大小写的.如果 abc = "Laptop",它不会找到它//要使其不区分大小写,您需要编辑 RegExp 构造函数//到这个: new RegExp("^"+abc+",|, "+abc+"(?!\w)", "i")userdetails.find({"product":new RegExp("^"+abc+",|, "+abc+"(?!\w)")}).toArray(function(err,result){if (err) console.log ("error:"+err);别的{//如果你想要长度控制台日志(结果.长度);//如果你真的想看到结果for (var i = 0; i < result.length; i++){控制台日志(结果 [i]);}}});

i am having data in mongodb like that

[

{
  "name":"silvester",
  "product":"laptop,iphone,mobile,phone"
},

{
   "name":"john",
   "product":"cycle,bus,phone,laptop"
},

{
   "name":"franklin",
   "product":"cycle,phone"
}

]

How to find that laptop is in product key. if product key look like this

{
"name":"XXX",
"product":"laptop"
}

I can easily find that name by using this db.collection.find("product":"laptop");

So how to find this?

Also let me know this three website name running under using backbone.js and node.js and mongodb technology such as www.trello.com . sorry for my worst english..

解决方案

Using regex with mongodb

This worked for me

db.collection.find({"product": /laptop/})

Updated Answer

If you wish to use variables, try something like this:

var abc = "laptop";
// other stuff
userdetails.find({"product":new RegExp(abc)}).toArray(function(err,result){
  if (err) console.log ("error: "+err);
  else 
  {    
    // if you want the length
    console.log(result.length);
    // if you actually want to see the results
    for (var i = 0; i < result.length; i++)
    {
      console.log(result[i]);
    }
  }
}); 

Updated One More Time

var abc = "laptop";
// other stuff

// note this is case sensitive.  if abc = "Laptop", it will not find it
// to make it case insensitive, you'll need to edit the RegExp constructor
// to this: new RegExp("^"+abc+",|, "+abc+"(?!\w)", "i")

userdetails.find({"product":new RegExp("^"+abc+",|, "+abc+"(?!\w)")}).toArray(function(err,result){
  if (err) console.log ("error: "+err);
  else 
  {    
    // if you want the length
    console.log(result.length);
    // if you actually want to see the results
    for (var i = 0; i < result.length; i++)
    {
      console.log(result[i]);
    }
  }
}); 

这篇关于如何在mongodb的键值对中找到特定的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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