Get Start with Mongodb
23 Aug 2017 | Database MEAN MongodbDatabases
-
database: can have relations between tables
-
relational db – non-relational db, tables – collections, object – document
Mongodb
-
Find how to download on mongodb website, install using homebrew is recommended.
-
which mongo
check the installation directory of mongodb -
Node.js: a platform
-
Packages in node: file, request handling, express
-
Run mongodb on mac: mongod
-
if it shutting down, maybe permission problem, using sudo su, then mongod
-
in a new terminal, mongo, it is running
-
-
Insert examples: db.collectionName.functionName
//insert 1 db.trainees.insert({ "id":2, "name":"yy2", "place":"nj2", "phone":"1232" }) //insert many db.trainees.insertMany([ { "id":12, "name":"yy12", "place":"nj12", "phone":"12312" }, { "id":11, "name":"yy11", "place":"nj11", "phone":"12311" } ])
-
Retrieve examples
//retrieve and format db.trainees.find().pretty()
-
Find specific document
db.trainees.find({"id":1,"place":"nj"}).pretty() //or db.trainees.find({ $or:[ {"id":11}, {"place":"nj"} ] }).pretty()
-
Limit show what props of result, second param in find(),
"_id":0
hide the auto-iddb.trainees.find({},{"id":1,"place":"nj","_id":0}).pretty()
-
More
$
db.trainees.find({ "id":{$gt:2}}).pretty() db.trainees.find({ "id":{$in:[1,11]}}).pretty()
-
Update: set
db.trainees.update({"id":12},{ $set:{ "name": "newname" } }) db.trainees.update({"id":12},{ $set:{ "org": "xyorg" } }) db.trainees.update({},{ $set:{ "org": "xyorg" } },{"multi":true}) //unset db.trainees.update({},{ $unset:{ "org": "" } },{"multi":true})
-
remove
db.trainees.remove({"id":12});
Comments