Open
Code
Home
Create
List
Home
Create
List
Edit
Title
Content
Starting MongoDB Server: mongod (start the MongoDB server) mongo (connect to the MongoDB shell) Database Operations: show dbs (display all databases) use mydb (create or switch to the "mydb" database) db (display the current database name) Collection Operations: db.name.insert({"name":"Daniel"}) (insert a document into the "name" collection) show collections (show all collections in the current database) db.name.drop() (drop the "name" collection) db.dropDatabase() (drop the current database) Creating a Collection: db.createCollection("test", {capped:true, size: 500, max:15, autoIndexId: true}) (create a collection with options) Inserting Documents: db.test.insertMany([{"database": ["mySql", "postgreSql", "mongoDB"]},{"mongoDB":"noSql"}]) (insert multiple documents into the "test" collection) Viewing Documents: db.test.find().pretty() (view all documents in the "test" collection) db.test2.find({}, {flag:1}).pretty() (select specific fields to display) Updating Documents: db.test.updateOne({"_id" : ObjectId("64c0f2da8215855ad6633e64")},{"$set": {"flag": 100}}) (update a document) db.test2.updateOne({"_id" : ObjectId("64c0f2da8215855ad6633e64")},{"$set": {"flag2": 300}}) (update with $set) Querying Documents: db.collection.find() (find all documents in a collection) db.collection.find({"flag": 100}).pretty() (find documents with a specific value) db.collection.find({"flag":{$gt: 99}}) (find documents where "flag" is greater than 99) db.collection.find({"flag":{$lt: 101}}) (find documents where "flag" is less than 101) db.collection.find({"flag":{$eq: 100}}) (find documents where "flag" is equal to 100) db.test2.find({ $or: [ {"flag" : {$eq: 100} } , {"test": {$ne : 50}} ] }) (find documents with $or condition) db.test2.find({ $and: [ {"flag" : {$eq: 100} } , {"test": {$ne : 50}} ] }) (find documents with $and condition) Deleting Documents: db.collection.deleteOne({"mongoDB":"noSql"}) (delete a document) db.collection.deleteMany({}) (delete all documents in a collection) Sorting Documents: db.test2.find().sort({flag:1}).pretty() (sort documents in ascending order of "flag" field) db.test2.find().sort({flag:-1}).pretty() (sort documents in descending order of "flag" field) Aggregation Pipeline (or Group By): $match: Filter documents. $group: Group documents by a specified key. $sum: Calculate the sum of numeric values. $avg: Calculate the average of numeric values. Examples: db.orders.aggregate([ {$match : {size: "medium"}} , {$group: { _id : "$name", totalQuantity : {$sum : "$quantity" } }} ]) (aggregate with match and group) db.test2.aggregate([ {$match : {flag: {$lt: 500}}} , {$group: { _id : "$flag", totalQuantity : {$sum : "$flag" } }} ]) (aggregate with match and group) db.test2.aggregate([ {$match : {}} , {$group: { _id : "$flag", totalQuantity : {$avg : "$flag" } }} ]) (aggregate with group only) Miscellaneous: new_id = ObjectId() (generate a new ObjectId) db.test2.insertOne({"_id" : new_id, "address" : { "street" : "1234" }}) (insert a document with a new ObjectId) ObjectId("62f4fcd8132e8eb2bce938c8") (example ObjectId)
Image
Category
Categories
Front-End
Back-End
Database
DevOps
Others
Password
Back to List