Mongoose
for ensuring structure in the data{
"_id": ObjectId(8df38ad8902c),
"title": "MongoDB",
"description": "MongoDB is NoSQL database",
"tags": ["mongodb", "database", "NoSQL"],
"likes": 100,
"comments": [
{ "user":"lover", "comment": "Great book!" },
{ "user":"hater", "comment": "Worst ever!" }]
}
_id
field: primary key
_id
is automatically added with a unique ObjectId
valueMongoDB document
RDB relation
show dbs;
use demo;
show collections;
db.books.insertOne({title: "MongoDB", likes: 100});
db.books.find();
show collections;
show dbs;
db.books.insertMany([{title: "a"}, {title: "b"}]);
db.books.find();
db.books.find({likes: 100});
db.books.find({likes: {$gt: 10}});
db.books.updateOne({title: "MongoDB"}, {$set: { likes: 200 }});
db.books.find();
db.books.deleteOne({title: "a"});
db.books.drop();
show collections;
show dbs;
mongo
: start MongoDB shelluse <dbName>
: use the databaseshow dbs
: show list of databasesshow collections
: show list of collectionsdb.colName.drop()
: delete colName
collectiondb.dropDatabase()
: delete current databaseinsertOne()
, insertMany()
findOne()
, find()
updateOne()
, updateMany()
deleteOne()
, deleteMany()
insertX(doc(s))
db.books.insertOne({title: "MongoDB", likes: 100})
db.books.insertMany([{title: "a"}, {title: "b"}])
findX(condition)
db.books.findOne({likes: 100})
db.books.find({$and: [{likes: {$gte: 10}}, {likes: {$lt: 20}}]})
findOne()
returns the first (?) matching document for multiple matches$or
, $not
, $gt
, $ne
, …updateX(condition, update_op)
db.books.updateOne({title: "MongoDB"}, {$set: {title: "MongoDB II"}})
db.books.updateMany({title: "MongoDB"}, {$inc: {likes: 1}})
$mul
(multiply), $unset
(remove the field), …deleteX(condition)
db.books.deleteOne({title: "MongoDB"})
db.books.deleteMany({likes: {$lt: 100}})
db.books.createIndex({title:1, likes:-1})