A list of basic Mongo commands and Mongoose commands
(click to expand/hide)
- activate local server port
- connect with mongo shell
-
mongosh
(click to expand/hide)
- Open a new terminal
-
sudo pkill -f mongod
- Enter you password
(click to expand/hide)
-
show dbs
(click to expand/hide)
-
use <database name>
-
db.<database name>.insertOne( { name: "tim", age: 26 } )
- if database does not exist, it will create one.
-
db.<database name>.insertOne( { name: "tim", age: 26, achievement:[ { title: "cap", year: 2021 }, { title: "gold", year: 2023 } ] } )
(click to expand/hide)
-
db.<database name>.find( { name: "tim", age: 26 } )
- find data in database that has age > 10
-
db.<database name>.find( { age: {$gt: 10} } )
- find data in database that has age > 10 and return with name and id
- 0 = do not show, 1 = show
-
db.<database name>.find( { age: {$gt: 10} }, { _id: 0, name: 1 } )
(click to expand/hide)
-
db.<database name>.updateOne( { _id: 1 }, { $set: {title: 15} } )
(click to expand/hide)
- switch to the database
-
use <database name>
- delete the database
-
db.dropDatabase()
-
db.<database name>.deleteOne( { _id: 1 } )
- delete successful if return acknowledged: true, and deletedCount: 1
(click to expand/hide)
-
npm i mongoose
(click to expand/hide)
- require package
-
const mongoose = require("mongoose");
-
- connect to mongoDB database
- connect to database with local host
-
mongoose.connect("mongodb://localhost:27017/<database name>", { useNewUrlParser: true });
(click to expand/hide)
-
mongoose.connection.close();
(click to expand/hide)
-
const <schema name> = new mongoose.Schema({ name: { type: String, required: [true, "Please check your data entry, no name specified!"] }, rating: { type: Number, min: 1, max: 10 }, review: String });
(click to expand/hide)
-
const schema1 = new mongoose.Schema({ name: { type: String, required: [true, "Please check your data entry, no name specified!"] }, rating: { type: Number, min: 1, max: 10 }, review: String }); const schema2 = new mongoose.Schema({ name: String, age: Number, FavouriteBook: schema1 });
(click to expand/hide)
-
const <schema name> = new mongoose.Schema({ name: String, rating: Number, review: String });
-
const <Model name> = mongoose.model("<Database name> without s", <schema name>);
- model name's first letter usually written in Capital letter
-
const obj = new <Model name>({ <key>: <value>, <key>: <value>, <key>: <value> }); const obj.save();
-
const obj1 = new <Model name>({ <key>: <value>, <key>: <value>, <key>: <value> }); <Model name>.insertMany([obj1, obj2, obj3]) .then(function(){ console.log("Successfully add item list to database."); }) .catch(function(err){ console.log(err); }); // Alternate version /* <Model name>.insertMany([obj1, obj2, obj3]) .then(() => { console.log("Successfully add item list to database."); }) .catch((err) => { console.log(err); }); */
(click to expand/hide)
-
<Model name>.find({}) .then(function(<result name>){ console.log("Successfully add item list to database."); }) .catch(function(err){ console.log(err); });
(click to expand/hide)
-
<Model name>.updateOne({_id: "123123"} , {name: "newName"}, function(err){ if(err){ console.log(err); }else{ console.log("Successfully updated the document."); });
(click to expand/hide)
-
<Model name>.deleteOne({_id: "123123"}) .then(function(){ console.log("Successfully deleted item"); }) .catch(function(err){ console.log(err); });
-
<Model name>.deleteMany({name: "John"}) .then(function(){ console.log("Successfully deleted items"); }) .catch(function(err){ console.log(err); });