In order to try various queries or to check the performance of the database, we need to have large enough documents; here we
will create a couple of large documents and try to query those with different
operators and functions available in MongoDB
In next article of MongoDB will be learn Indexing and explain plan.
In the below examples we will create two collections
techno_col and tech_large with 2000 and 15000 documents in it. Insertion might
take some time depending on number of documents
> for (i = 0; i < 2000; i++) { db.techno_col.save({num: i});
}
WriteResult({ "nInserted" : 1 })
>
> db.techno_col.count();
2000
>
> for(i = 0; i < 15000; i++)
{db.tech_large.save({emp_id : i});};
WriteResult({ "nInserted" : 1 })
>
> db.tech_large.count();
15000
>
Now try to query documents from techno_col
collections,
> db.techno_col.find()
{ "_id" :
ObjectId("5c5d66a310f3108c3c83d574"), "num" : 0 }
{ "_id" :
ObjectId("5c5d66a310f3108c3c83d575"), "num" : 1 }
{ "_id" : ObjectId("5c5d66a310f3108c3c83d576"),
"num" : 2 }
{ "_id" :
ObjectId("5c5d66a310f3108c3c83d577"), "num" : 3 }
{ "_id" :
ObjectId("5c5d66a310f3108c3c83d578"), "num" : 4 }
{ "_id" :
ObjectId("5c5d66a310f3108c3c83d579"), "num" : 5 }
{ "_id" : ObjectId("5c5d66a310f3108c3c83d57a"),
"num" : 6 }
{ "_id" :
ObjectId("5c5d66a310f3108c3c83d57b"), "num" : 7 }
{ "_id" :
ObjectId("5c5d66a310f3108c3c83d57c"), "num" : 8 }
{ "_id" :
ObjectId("5c5d66a310f3108c3c83d57d"), "num" : 9 }
{ "_id" :
ObjectId("5c5d66a310f3108c3c83d57e"), "num" : 10 }
{ "_id" :
ObjectId("5c5d66a310f3108c3c83d57f"), "num" : 11 }
{ "_id" :
ObjectId("5c5d66a310f3108c3c83d580"), "num" : 12 }
{ "_id" :
ObjectId("5c5d66a310f3108c3c83d581"), "num" : 13 }
{ "_id" : ObjectId("5c5d66a310f3108c3c83d582"),
"num" : 14 }
{ "_id" :
ObjectId("5c5d66a310f3108c3c83d583"), "num" : 15 }
{ "_id" :
ObjectId("5c5d66a310f3108c3c83d584"), "num" : 16 }
{ "_id" :
ObjectId("5c5d66a310f3108c3c83d585"), "num" : 17 }
{ "_id" : ObjectId("5c5d66a310f3108c3c83d586"),
"num" : 18 }
{ "_id" :
ObjectId("5c5d66a310f3108c3c83d587"), "num" : 19 }
Type "it" for more
>
Here we can see only 20 documents, it will
depend upon the platform, next documents can be seen using "it" as
suggested at the end of the result
> it
{ "_id" :
ObjectId("5c5d66a310f3108c3c83d588"), "num" : 20 }
{ "_id" :
ObjectId("5c5d66a310f3108c3c83d589"), "num" : 21 }
.....
{ "_id" :
ObjectId("5c5d66a310f3108c3c83d59b"), "num" : 39 }
Type "it" for more
>
Now try finding details for employees with emp_id
2000 from collection tech_large
> db.tech_large.find({emp_id: 2000});
{ "_id" :
ObjectId("5c5d69e110f3108c3c83e514"), "emp_id" : 2000 }
>
Find details
of num value greater than 1991
>
db.techno_col.find({num: {"$gt" :1991}});
{
"_id" : ObjectId("5c5d66a510f3108c3c83dd3c"),
"num" : 1992 }
{
"_id" : ObjectId("5c5d66a510f3108c3c83dd3d"),
"num" : 1993 }
{
"_id" : ObjectId("5c5d66a510f3108c3c83dd3e"),
"num" : 1994 }
{
"_id" : ObjectId("5c5d66a510f3108c3c83dd3f"),
"num" : 1995 }
{
"_id" : ObjectId("5c5d66a510f3108c3c83dd40"),
"num" : 1996 }
{
"_id" : ObjectId("5c5d66a510f3108c3c83dd41"),
"num" : 1997 }
{
"_id" : ObjectId("5c5d66a510f3108c3c83dd42"),
"num" : 1998 }
{
"_id" : ObjectId("5c5d66a510f3108c3c83dd43"),
"num" : 1999 }
>
Now try finding the employees between emp_id
5000 and 5009.
> db.tech_large.find({emp_id :
{"$gt" : 5000, "$lt" : 5009}});
{ "_id" :
ObjectId("5c5d69e410f3108c3c83f0cd"), "emp_id" : 5001 }
{ "_id" :
ObjectId("5c5d69e410f3108c3c83f0ce"), "emp_id" : 5002 }
{ "_id" :
ObjectId("5c5d69e410f3108c3c83f0cf"), "emp_id" : 5003 }
{ "_id" :
ObjectId("5c5d69e410f3108c3c83f0d0"), "emp_id" : 5004 }
{ "_id" :
ObjectId("5c5d69e410f3108c3c83f0d1"), "emp_id" : 5005 }
{ "_id" :
ObjectId("5c5d69e410f3108c3c83f0d3"), "emp_id" : 5007 }
{ "_id" :
ObjectId("5c5d69e410f3108c3c83f0d4"), "emp_id" : 5008 }
>
Use of $and and $or operators :
As I have only typical documents in the collection, results may
not differ, but these operators will be a handful
> db.techno_col.find({$and: [{_id :
ObjectId("5c5d66a410f3108c3c83d8ea")}, {num : 886}]})
{ "_id" :
ObjectId("5c5d66a410f3108c3c83d8ea"), "num" : 886 }
>
> db.techno_col.find({$or: [{_id :
ObjectId("5c5d66a410f3108c3c83d8ea")}, {num : 887}]})
{ "_id" :
ObjectId("5c5d66a410f3108c3c83d8ea"), "num" : 886 }
{ "_id" :
ObjectId("5c5d66a410f3108c3c83d8eb"), "num" : 887 }
>
In next article of MongoDB will be learn Indexing and explain plan.
0 comments:
Post a Comment