Showing posts with label MongoDB. Show all posts
Showing posts with label MongoDB. Show all posts

Friday, August 25, 2023

Mongo Shell Commands

 Following commands can be used in mongo db shell.

show dbs

  • list of all the databases

use shops

  • switch to a specific database or creates a database (shops) 

db.createCollection("products")

  • creating the collection (products)

show collections

  • display a list of all the collections 

 db.products.insertOne({"name":"prod_name1"})

  • insert a document into a collection

db.products.insertMany([{"name":"prod_name2"},{"name":"prod_name3"}])

  • insert many products in one command

db.products.insertOne({"name":"test_prod4","vendor":{"name":"vendor_1","address":"address_1"}})

  • insert many products in one command with multiple objects

db.student.insertOne({"name":"test_prod5",,"vendor":{"name":"vendor_1","address":{"address_line1":"line1","address_line2":"line2","city":"city"}}})

  • insert many products in one command with multiple objects

db.products.find()

  • retrieve all documents

db.products.update({name:"test_prod1"},{$set:{price:"120.00"}})

  • update a document in the "products" collection. updates first document only for name:"test_prod1" 

db.student.update({"name":"test_6"},{$set:{age:26}},{multi:true})

  • updating all document

db.student.update({_id:ObjectId("64dc5856e4cebb07084d8c45")},{$unset:{price:"120.00"}})

  • updating the document using object id and remove price attribute

db.teaches.update({"th_name":"test_th_2"},{$set:{student:[{"std_name":"test_1"},{"std_name":"test_2"}]}})

  • adding the Student object field

db.student.find({$and:[{"name":"test_4"},{"age":"40"}]})

  • AND operation. returns all objects with name =test_4 and age=40

db.student.find({$or:[{"name":"test_4"},{"name":"test_2"}]})

  • OR operation and returns name = test_2 or test_4

db.student.drop()

  • drop student collection

db.teaches.remove({"name":"t_name1"})

  • remove document name =t_name1

db.teaches.deleteOne({"name":"t_name2"})

  • remove one document with name=t_name2

db.student.aggregate([{$lookup:{from:"teaches",localField:"std_name",foreignField:"student.std_name",as:"studentDetails"}}])

  • aggregation operation in MongoDB to join the "student" collection with the "teaches" collection

db.mycol.find().pretty() 

  • return results in formatted way



Monday, June 13, 2016

Run MongoDB as a service

Following command can be used to run mongo db as service.

mongod.exe --install --dbpath "c:\mongo\data\db" --logpath "c:\mongo\data\log"

You can also set dbpath & logpath in "mongod.cfg" file and run the command as follows.

mongod.exe --install --config "c:\mongo\mongod.cfg"

Sample mongod.cfg

systemLog:
    destination: file
    path: C:\Program Files\MongoDB\Server\3.2\data\log\mongod.log
storage:
    dbPath: C:\Program Files\MongoDB\Server\3.2\data\db

In Windows, you can stop service in services management window. Run/Services

In Linux, ps -ef|grep mongo then kill -9 process_id