简体   繁体   中英

How to find the element based on its name in mongodb

I need to find the object whose name is "abc" from category collection in mongodb. Here is the my code

[{
    _id: 728e38e7,
    name: 'abc,def,ghi,klm,nmo',
    place: "mys"
},
{
    _id: 2788,
    name: 'djhd',
    place: "bang"
}]

try that

var query = { name: new RegExp('^' + abc) };
collection.find(query).toArray(function(err, items))

Using the mongo shell you can use find().

db.collection.find({"name" : /abc/}).

From this post : How to query MongoDB with "like"?

Below is the simplest method of using regex to find element based on name in mongodb :

db.collection.find({name:{$regex:"ABC",$options:"$i"}})

Here $i is used to make the search case insensitive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM