Contents of page >
1) First let's create records before using like statement (as in sql) in MongoDB
Let's create new collection and insert documents in it before using like statement >
> db.testCollection.insert({name: 'abc'})
> db.testCollection.insert({name: 'bcd'})
> db.testCollection.insert({name: 'def'})
|
First line above will create table (or collection) (if table already exists it will insert documents in it).
Read all documents of collection >
> db.testCollection.find()
{ "_id" : ObjectId("585008261127dea5ece72759"), "name" : "abc" }
{ "_id" : ObjectId("585008261127dea5ece7275a"), "name" : "bcd" }
{ "_id" : ObjectId("585008261127dea5ece7275b"), "name" : "def" }
>
|
3 documents were found.
2) Below find document in mongoDB is similar to like '%b%' in sql >
> db.testCollection.find({name : /b/})
{ "_id" : ObjectId("585008261127dea5ece72759"), "name" : "abc" }
{ "_id" : ObjectId("585008261127dea5ece7275a"), "name" : "bcd" }
>
|
2 documents were found.
3) Below find document in mongoDB is similar to like 'b%' in sql >
> db.testCollection.find({name : /^b/})
{ "_id" : ObjectId("585008261127dea5ece7275a"), "name" : "bcd" }
>
|
1 document was found.
4) Below find document in mongoDB is similar to like '%f' in sql >
> db.testCollection.find({name : /f$/})
{ "_id" : ObjectId("585008261127dea5ece7275b"), "name" : "def" }
>
|
1 document was found.
5) SUMMARY>
So in this mongoDB tutorial we learned how to use Using like statement (as in sql) in MongoDB
Below find document in mongoDB is similar to like '%b%' in sql >
> db.testCollection.find({name : /b/})
|
Below find document in mongoDB is similar to like 'b%' in sql >
> db.testCollection.find({name : /^b/})
|
Below find document in mongoDB is similar to like '%f' in sql >
> db.testCollection.find({name : /f$/})
|
Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>
What is MongoDB - A quick introduction to database
objectId in MongoDB
Create new database in mongoDB
How to Drop collection in MongoDB
Create new collection(table) in mongoDB
Labels:
MongoDB