Sunday, 15 June 2014

node.js - how to find specific string in key value pair in mongodb -



node.js - how to find specific string in key value pair in mongodb -

i having info in mongodb that

[ { "name":"silvester", "product":"laptop,iphone,mobile,phone" }, { "name":"john", "product":"cycle,bus,phone,laptop" }, { "name":"franklin", "product":"cycle,phone" } ]

how find laptop in product key. if product key

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

i can find name using db.collection.find("product":"laptop");

so how find this?

also allow me know 3 website name running under using backbone.js , node.js , mongodb technology such www.trello.com . sorry worst english..

using regex mongodb

this worked me

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

updated answer

if wish utilize variables, seek this:

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

updated 1 more time

var abc = "laptop"; // other stuff // note case sensitive. if abc = "laptop", not find // create case insensitive, you'll need edit regexp constructor // 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 want length console.log(result.length); // if want see results (var = 0; < result.length; i++) { console.log(result[i]); } } });

node.js mongodb

No comments:

Post a Comment