Skip to content

Commit bf4cd62

Browse files
author
developer-sujon
committed
split js file
1 parent 52e8338 commit bf4cd62

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1653
-131
lines changed

00.database-operations-basic.txt

Lines changed: 780 additions & 0 deletions
Large diffs are not rendered by default.

01-shell-important-commands.txt

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
mongod --help
2+
3+
> help
4+
db.help() help on db methods
5+
db.mycoll.help() help on collection methods
6+
sh.help() sharding helpers
7+
rs.help() replica set helpers
8+
help admin administrative help
9+
help connect connecting to a db help
10+
help keys key shortcuts
11+
help misc misc things to know
12+
help mr mapreduce
13+
14+
show dbs show database names
15+
show collections show collections in current database
16+
show users show users in current database
17+
show profile show most recent system.profile entries with time >= 1ms
18+
show logs show the accessible logger names
19+
show log [name] prints out the last segment of log in memory, 'global' is default
20+
use <db_name> set current database
21+
db.foo.find() list objects in collection foo
22+
db.foo.find( { a : 1 } ) list objects in foo where a == 1
23+
it result of the last line evaluated; use to further iterate
24+
DBQuery.shellBatchSize = x set default number of items to display on shell
25+
exit quit the mongo shell
26+
27+
28+
29+
> use shop
30+
switched to db shop
31+
> db.help()
32+
DB methods:
33+
db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [just calls db.runCommand(...)]
34+
db.aggregate([pipeline], {options}) - performs a collectionless aggregation on this database; returns a cursor
35+
db.auth(username, password)
36+
db.cloneDatabase(fromhost)
37+
db.commandHelp(name) returns the help for the command
38+
db.copyDatabase(fromdb, todb, fromhost)
39+
db.createCollection(name, {size: ..., capped: ..., max: ...})
40+
db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})
41+
db.createUser(userDocument)
42+
db.currentOp() displays currently executing operations in the db
43+
db.dropDatabase()
44+
db.eval() - deprecated
45+
db.fsyncLock() flush data to disk and lock server for backups
46+
db.fsyncUnlock() unlocks server following a db.fsyncLock()
47+
db.getCollection(cname) same as db['cname'] or db.cname
48+
db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections
49+
db.getCollectionNames()
50+
db.getLastError() - just returns the err msg string
51+
db.getLastErrorObj() - return full status object
52+
db.getLogComponents()
53+
db.getMongo() get the server connection object
54+
db.getMongo().setSlaveOk() allow queries on a replication slave server
55+
db.getName()
56+
db.getPrevError()
57+
db.getProfilingLevel() - deprecated
58+
db.getProfilingStatus() - returns if profiling is on and slow threshold
59+
db.getReplicationInfo()
60+
db.getSiblingDB(name) get the db at the same server as this one
61+
db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
62+
db.hostInfo() get details about the server's host
63+
db.isMaster() check replica primary status
64+
db.killOp(opid) kills the current operation in the db
65+
db.listCommands() lists all the db commands
66+
db.loadServerScripts() loads all the scripts in db.system.js
67+
db.logout()
68+
db.printCollectionStats()
69+
db.printReplicationInfo()
70+
db.printShardingStatus()
71+
db.printSlaveReplicationInfo()
72+
db.dropUser(username)
73+
db.repairDatabase()
74+
db.resetError()
75+
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj: 1}
76+
db.serverStatus()
77+
db.setLogLevel(level,<component>)
78+
db.setProfilingLevel(level,slowms) 0=off 1=slow 2=all
79+
db.setWriteConcern(<write concern doc>) - sets the write concern for writes to the db
80+
db.unsetWriteConcern(<write concern doc>) - unsets the write concern for writes to the db
81+
db.setVerboseShell(flag) display extra information in shell output
82+
db.shutdownServer()
83+
db.stats()
84+
db.version() current version of the server
85+
86+
87+
88+
89+
> show collections
90+
products
91+
> db.products.help()
92+
DBCollection help
93+
db.products.find().help() - show DBCursor help
94+
db.products.bulkWrite( operations, <optional params> ) - bulk execute write operations, optional parameters are: w, wtimeout, j
95+
db.products.count( query = {}, <optional params> ) - count the number of documents that matches the query, optional parameters are: limit, skip, hint, maxTimeMS
96+
db.products.copyTo(newColl) - duplicates collection by copying all documents to newColl; no indexes are copied.
97+
db.products.convertToCapped(maxBytes) - calls {convertToCapped:'products', size:maxBytes}} command
98+
db.products.createIndex(keypattern[,options])
99+
db.products.createIndexes([keypatterns], <options>)
100+
db.products.dataSize()
101+
db.products.deleteOne( filter, <optional params> ) - delete first matching document, optional parameters are: w, wtimeout, j
102+
db.products.deleteMany( filter, <optional params> ) - delete all matching documents, optional parameters are: w, wtimeout, j
103+
db.products.distinct( key, query, <optional params> ) - e.g. db.products.distinct( 'x' ), optional parameters are: maxTimeMS
104+
db.products.drop() drop the collection
105+
db.products.dropIndex(index) - e.g. db.products.dropIndex( "indexName" ) or db.products.dropIndex( { "indexKey" : 1 } )
106+
db.products.dropIndexes()
107+
db.products.ensureIndex(keypattern[,options]) - DEPRECATED, use createIndex() instead
108+
db.products.explain().help() - show explain help
109+
db.products.reIndex()
110+
db.products.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
111+
e.g. db.products.find( {x:77} , {name:1, x:1} )
112+
db.products.find(...).count()
113+
db.products.find(...).limit(n)
114+
db.products.find(...).skip(n)
115+
db.products.find(...).sort(...)
116+
db.products.findOne([query], [fields], [options], [readConcern])
117+
db.products.findOneAndDelete( filter, <optional params> ) - delete first matching document, optional parameters are: projection, sort, maxTimeMS
118+
db.products.findOneAndReplace( filter, replacement, <optional params> ) - replace first matching document, optional parameters are: projection, sort, maxTimeMS, upsert, returnNewDocument
119+
db.products.findOneAndUpdate( filter, update, <optional params> ) - update first matching document, optional parameters are: projection, sort, maxTimeMS, upsert, returnNewDocument
120+
db.products.getDB() get DB object associated with collection
121+
db.products.getPlanCache() get query plan cache associated with collection
122+
db.products.getIndexes()
123+
db.products.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
124+
db.products.insert(obj)
125+
db.products.insertOne( obj, <optional params> ) - insert a document, optional parameters are: w, wtimeout, j
126+
db.products.insertMany( [objects], <optional params> ) - insert multiple documents, optional parameters are: w, wtimeout, j
127+
db.products.mapReduce( mapFunction , reduceFunction , <optional params> )
128+
db.products.aggregate( [pipeline], <optional params> ) - performs an aggregation on a collection; returns a cursor
129+
db.products.remove(query)
130+
db.products.replaceOne( filter, replacement, <optional params> ) - replace the first matching document, optional parameters are: upsert, w, wtimeout, j
131+
db.products.renameCollection( newName , <dropTarget> ) renames the collection.
132+
db.products.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
133+
db.products.save(obj)
134+
db.products.stats({scale: N, indexDetails: true/false, indexDetailsKey: <index key>, indexDetailsName: <index name>})
135+
db.products.storageSize() - includes free space allocated to this collection
136+
db.products.totalIndexSize() - size in bytes of all the indexes
137+
db.products.totalSize() - storage allocated for all data and indexes
138+
db.products.update( query, object[, upsert_bool, multi_bool] ) - instead of two flags, you can pass an object with fields: upsert, multi
139+
db.products.updateOne( filter, update, <optional params> ) - update the first matching document, optional parameters are: upsert, w, wtimeout, j
140+
db.products.updateMany( filter, update, <optional params> ) - update all matching documents, optional parameters are: upsert, w, wtimeout, j
141+
db.products.validate( <full> ) - SLOW
142+
db.products.getShardVersion() - only for use with sharding
143+
db.products.getShardDistribution() - prints statistics about data distribution in the cluster
144+
db.products.getSplitKeysForChunks( <maxChunkSize> ) - calculates split points over all chunks and returns splitter function
145+
db.products.getWriteConcern() - returns the write concern used for any operations on this collection, inherited from server/db if set
146+
db.products.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the collection
147+
db.products.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the collection
148+
db.products.latencyStats() - display operation latency histograms for this collection

02.InsertData.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//Insert Document
2+
db.getCollection("employee").insert({
3+
name: "Mohammad Sujon",
4+
designation: "web developer",
5+
salary: 20000,
6+
});
7+
8+
/*
9+
executed result
10+
for single document => WriteResult({ "nInserted" : 1 })
11+
for multiple document => {
12+
"writeErrors" : [],
13+
"writeConcernErrors" : [],
14+
"nInserted" : 3.0,
15+
"nUpserted" : 0.0,
16+
"nMatched" : 0.0,
17+
"nModified" : 0.0,
18+
"nRemoved" : 0.0,
19+
"upserted" : []
20+
}*/
21+
22+
//Insert single Document
23+
db.getCollection("employee").insertOne({
24+
name: "Mohammad Sujon",
25+
designation: "web developer",
26+
salary: 20000,
27+
});
28+
29+
/*
30+
executed result
31+
{
32+
"acknowledged" : true,
33+
"insertedId" : ObjectId("6474d6d880389505350476aa")
34+
}
35+
}*/
36+
37+
//Inset Multiple Document
38+
db.getCollection("employee").insertMany([
39+
{
40+
name: "Hamim",
41+
designation: "Marketing",
42+
salary: 80000,
43+
},
44+
{
45+
name: "Poros",
46+
designation: "Email Sender",
47+
salary: 10000,
48+
},
49+
]);
50+
51+
/*
52+
executed result
53+
{
54+
"acknowledged" : true,
55+
"insertedIds" : [
56+
ObjectId("6474d74380389505350476ab"),
57+
ObjectId("6474d74380389505350476ac")
58+
]
59+
}
60+
}*/

03.FindData.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//Find Document
2+
db.getCollection("employee").find();
3+
4+
/*
5+
executed result (all documents)
6+
{
7+
"_id" : ObjectId("6474d74380389505350476ab"),
8+
"name" : "Mohammad",
9+
"designation" : "Developer",
10+
"salary" : 80000
11+
}
12+
{
13+
"_id" : ObjectId("6474d74380389505350476ac"),
14+
"name" : "Sujon",
15+
"designation" : "Email Sender",
16+
"salary" : 10000
17+
}
18+
}*/
19+
20+
db.getCollection("employee").findOne();
21+
/*
22+
executed result (first document)
23+
{
24+
"_id" : ObjectId("6474d74380389505350476ab"),
25+
"name" : "Mohammad",
26+
"designation" : "Developer",
27+
"salary" : 80000
28+
}
29+
}*/
30+
31+
db.getCollection("employee").aggregate([]);
32+
/*
33+
executed result (all documents)
34+
{
35+
"_id" : ObjectId("6474d74380389505350476ab"),
36+
"name" : "Mohammad",
37+
"designation" : "Developer",
38+
"salary" : 80000
39+
}
40+
{
41+
"_id" : ObjectId("6474d74380389505350476ac"),
42+
"name" : "Sujon",
43+
"designation" : "Email Sender",
44+
"salary" : 10000
45+
}
46+
}*/

04query-comparison.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
$eq
3+
Matches values that are equal to a specified value.
4+
$ne
5+
Matches all values that are not equal to a specified value.
6+
$gt
7+
Matches values that are greater than a specified value.
8+
$gte
9+
Matches values that are greater than or equal to a specified value.
10+
$lt
11+
Matches values that are less than a specified value.
12+
$lte
13+
Matches values that are less than or equal to a specified value.
14+
$in
15+
Matches any of the values specified in an array.
16+
$nin
17+
Matches none of the values specified in an array.
18+
*/

05.FindByWhereQuery.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//Find by Where Query
2+
db.getCollection("employee").find({ salary: { $gte: 3000 } });
3+
4+
/*
5+
executed result (all match documents)
6+
{
7+
"_id" : ObjectId("6474d74380389505350476ab"),
8+
"name" : "Mohammad",
9+
"designation" : "Developer",
10+
"salary" : 3000
11+
}
12+
{
13+
"_id" : ObjectId("6474d74380389505350476ac"),
14+
"name" : "Sujon",
15+
"designation" : "Email Sender",
16+
"salary" : 3500
17+
}
18+
}*/
19+
20+
db.getCollection("employee").aggregation([{ salary: { $gte: 3000 } }]);
21+
22+
/*
23+
executed result (all match documents)
24+
{
25+
"_id" : ObjectId("6474d74380389505350476ab"),
26+
"name" : "Mohammad",
27+
"designation" : "Developer",
28+
"salary" : 3000
29+
}
30+
{
31+
"_id" : ObjectId("6474d74380389505350476ac"),
32+
"name" : "Sujon",
33+
"designation" : "Email Sender",
34+
"salary" : 3500
35+
}
36+
}*/

06.FindbyIn.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//Find By In
2+
db.getCollection("employee").find({ name: { $in: ["Sujon", "Rakib"] } });
3+
4+
/*
5+
executed result (all match documents)
6+
{
7+
"_id" : ObjectId("6474d74380389505350476ab"),
8+
"name" : "Sujon",
9+
"designation" : "Developer",
10+
"salary" : 3000
11+
}
12+
{
13+
"_id" : ObjectId("6474d74380389505350476ac"),
14+
"name" : "Rakib",
15+
"designation" : "Email Sender",
16+
"salary" : 3500
17+
}
18+
}*/
19+
20+
db.getCollection("employee").aggregate([
21+
{ $match: { skills: { $in: ["react", "node"] } } },
22+
]);
23+
24+
/*
25+
executed result (all match documents)
26+
{
27+
"_id" : ObjectId("6474d74380389505350476ab"),
28+
"name" : "Mohammad",
29+
"designation" : "Developer",
30+
"salary" : 3000,
31+
"skills" : [
32+
"js",
33+
"react",
34+
"node"
35+
]
36+
}
37+
}*/

07.FindAndLimit.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//Find and Limit Document
2+
db.getCollection("employee").find({}).limit(2);
3+
4+
/*
5+
executed result
6+
{
7+
"_id" : ObjectId("6474d74380389505350476ab"),
8+
"name" : "Mohammad",
9+
}
10+
{
11+
"_id" : ObjectId("6474d74380389505350476ac"),
12+
"name" : "Sujon",
13+
}
14+
}*/
15+
16+
db.getCollection("employee").aggregate([{ $limit: 1 }]);
17+
18+
/*
19+
executed result
20+
{
21+
"_id" : ObjectId("6474d74380389505350476ab"),
22+
"name" : "Mohammad",
23+
}
24+
}*/

0 commit comments

Comments
 (0)