Linux

MongoDb with Python $lt and $gt

In the Pymongo -Insert chapter, we learned how to insert the records in the collection of books database. Now we are going to discuss the functionality of $gt and $lt.

Click to view JSON File
If you look at JSON file, you will see that, new field type has been added, Which will be helpful for understanding the functionality of $gt and $lt. The $gt and $lt means greater than and less than respectively.
Now we discuss about how to use greater than and less than inside Pymongo. Just look at new JSON file records, we stored the JSON document in book_collection4.
MongoDB Document find book_collection4
MongoDB Document find book_collection4

So our aim to find the records contain type = "Social" and ID > 2 and ID < 7 .
Let discuss out new code.
MongoDB $gt and $lt
MongoDB $gt and $lt
Click to view code

This time query in code looking for "type" = "Social" with ID greater than 2 and ID less than 7, order does not matter. The code {'$gt':2, '$lt':7}} in line 8 is document basically for value {'$gt':2, '$lt':7}} ID is the key. Let us check the out-put.
MongoDB $gt and $lt output
MongoDB $gt and $lt code output

In above code we have used type as well as ID, if you want to print the records with ID greater than 2 and ID less than 7. Use the following code.
MongoDB using $gt and $lt
MongoDB using $gt and $lt
Click to view code

Let us check the Out-put.
MongoDB $gt and $lt code output
MongoDB $gt and $lt code output

So we are getting output according to our code.





admin