Testing new waters. My first NoSQL db was InfluxDB. It is a Time based No SQL type db.
This time I wanted to taste the all famous MongoDB. I want to keep the basics handy. So I write as I learn.
Basic Commands ☝
Launch MongoDB:
~ mongo
See existing databases
show dbs
Create a database
use db_name
Return: Creates and switches to it.
Note: It won't show in db list unless we create at least one entry (a collection)
Use a database
use db_name
Returns: switched to db db_name
See all collections in that database
show collections
Drop a Database
db.dropDatabase()
Create a collection
db.createCollection("some_name")
Drop a collection
db.collection_name.drop()
Returns : true
Insert data into collection and verify
db.collection_name.insert({key1:value1, ......})
Let's try adding one more. Something to note here. You can use find to query.
You can use pretty() to see a well formatted output.
Querying Data
In this, we will explore various ways to find data.
Methods available: find(), findOne()
* find() is == all()
* find({key:value}) == filter(key=value)
To find exact data matching results, we just mention the key and value
({key:value})
If not exact but some condition :
LT
( { key: $lt:value } ) Note: spaces are only to show clearly.
GT
( { key: $gt:value } )
LTE
( { key: $lte:value } )
GTE
( { key: $gte:value } )
NE
( { key: $ne:value } )
IN (Value in array)
( { key: $in:value } )
NIN
( { key: $nin:value } )
Example Queries:
1. Find user by exact age and greater than.
2. Find from a list of choices. Here , I provide a list of options. All the ones matching exactly are listed.
3. We can combine filters with AND and OR
4. NOR
No comments:
Post a Comment