Skip to content

codeStroke/mongoexample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 

Repository files navigation

MongoDB by Example

Collection of MongoDB tips and tricks, want to add your tips? Checkout contributing.md

P.S: All these commands are tested on MongoDB shell version v3.4.2.

MongoDB Package Component

Mongo Shell

MongoDB CRUD Operations

each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field.

Import data into the collection using JSON generated By Mongoexport

mongoimport --db <databaseName> --collection <collectionName> --drop --file <jsonFile>.json

Import data into the collection using regular JSON

mongoimport --db <databaseName> --collection <collectionName> --drop --file <jsonFile>.json --jsonArray

Restore data into Atlas using dump

mongorestore --ssl --host atlas-host1:27017,atlas-host2:27017,atlas-host3:27017 -u usename -p mypassword dump

Import data into Atlas using mongoimport

mongoimport --host atlas-host1:27017,atlas-host2:27017,atlas-host3:27017 --ssl -u username -p 'password' --authenticationDatabase admin --db <database> --collection <collection> --file <file.json>

Show list of Databases

show dbs

Copy Database

db.copyDatabase(fromdb, todb)

Copy Database Using Host and user credential

db.copyDatabase(fromdb, todb, fromhost, username, password, mechanism)

Insert a single document

db.mycollection.insertOne(
   { bookName: "Indian History", price: 100, tags: ["History"], size: { h: 28, w: 35.5, uom: "cm" } }
  )

Insert multiple documents

db.mycollection.insertMany([
   { bookName: "Modern science", price: 500.34, tags: ["science"], size: { h: 28, w: 35.5, uom: "cm" } },
   { bookName: "Computer Design", price: 300, tags: ["computer"], size: { h: 28, w: 35.5, uom: "cm" } },
  )]

Inserts a document or documents into a collection

db.mycollection.insert([
   { bookName: "Modern science", price: 500.34, tags: ["science"], size: { h: 28, w: 35.5, uom: "cm" } },
   { bookName: "Computer Design", price: 300, tags: ["computer"], size: { h: 28, w: 35.5, uom: "cm" } },
  ])
    
    
  
db.mycollection.insert(
   { bookName: "Indian History", price: 100, tags: ["History"], size: { h: 28, w: 35.5, uom: "cm" } }
   )

Update a field to an empty array in all documents

db.mycollection.update(
{}, 
{$set : {fieldName:[]} }, {multi:true}
)

Insert a new field into a collection

db.mycollection.updateMany(
{}, 
{$set :{"newFieldName":""}}
)

Find value inside array

field : studentMarks[{rollNo:1,marks:10},{rollNo:2,marks:5},{rollNo:3,marks:10}]

//query to get documents where students got 10 marks

db.mycollection.find( 
{ "studentMarks": { $elemMatch: { "marks": 10 } } } 
)

Find documents where field value is empty

//query to get documents where name is empty

db.mycollection.find( 
{ "name": ""} 
)

Find documents where field value is non empty

//query to get documents where name is non empty

db.mycollection.find( 
{ "name": {$ne : ""}} 
)

Releases

No releases published

Packages

No packages published