sudo mongod --config /etc/mongod
Login to DB
mongo
Basics Queries
show dbs- Create database
use mycustomers- Show current DB
db- Create User
user: "sanjay",
pwd: "1234",
roles: ["readwrite", "dbadmin"]
})```
- Create Collection(Tables)
db.createCollection('customers')- Show Collections
show collections- Insert data into the collections
db.customers.insert(
[
{first_name: "vin", last_name: "diesel"},
{first_name: "powel", last_name: "walker"}
]
)```
- Display the document(data) in the collections(table)
db.customers.find()- Update
db.customers.update( {first_name: "vin"},
{first_name:"vinvin", gender: "male"} );```
Notice that
updatewill have a new values asfirst_nameandgenderdiscardedlast_namefor that use$setas follows.
$set
db.customers.update({first_name: "vinvin"}, {$set:{phno: "123123"}} )```
- Query particular document
db.customers.find({first_name:"powel"})
pretty()db.news.find().pretty()
- Update two or more fields
db.customers.update({first_name: "powel"},
{$set: {phno: 3434, gender: "male"}})```
- Update whole document
db.news.updateMany({}, {$set: {tag: ["sports", "technology"]}})
$unsetoperator delets a particular fileddb.news.update({title: "B"}, {$unset: {likes: 1}})
upsert
db.news.update({title: "D"},
{title: "D", description: "know more about D",
likes: 10, tag: ["sports", "technology"]},
{upsert: true})```
`$rename` updates the name of the field
```db.news.update({},
{rename: {tag: "tags"}})```
To update all
18 . remove
db.news.remove({title: "D"})
19 .
$gtdb.news.find({dislikes: {$gt: 19}})